To rename all files in current directory (append .bak to all of them):
ls -l | awk '{print "mv "$1" "$1".new"}' | sh
Thats doing an ls of all files , passing each name to awk as argument , renaming the awk input and passing the whole formatted output to shell
1. Renaming within the name:
ls -1 *name1* | awk '{print "mv "$1" "$1}' | sed s/name1/name2/2 | sh
2. remove only files:
ls -l * | grep -v drwx | awk '{print "rm "$9}' | sh
or with awk alone:
ls -l|awk '$1!~/^drwx/{print $9}'|xargs rm
Be careful when trying this out in your home directory. We remove files!
3. remove only directories
ls -l | grep '^d' | awk '{print "rm -r "$9}' | sh
or
ls -p | grep /$ | wk '{print "rm -r "$1}'
or with awk alone:
ls -l|awk '$1~/^d.*x/{print $9}'|xargs rm -r
Be careful when trying this out in your home directory. We remove things!
4. killing processes by name (in this example we kill the process called netscape):
kill `ps auxww | grep netscape | egrep -v grep | awk '{print $2}'`
or with awk alone:
ps auxww | awk '$0~/netscape/&&$0!~/awk/{print $2}' |xargs kill
It has to be adjusted to fit the ps command on whatever unix system you are on. Basically it is: "If the process is called netscape and it is not called 'grep netscape' (or awk) then print the pid"
No comments:
Post a Comment