Monday, January 14, 2008

Grepping the history = save time and energy

It happens to me almost everyday that I type a long command (combination of commands) and then after executing million other commands I need the long old command back again. What I used to do is hit up and down arrow keys on my key board and keep staring on the screen so that I don't miss the long command. This process is usually very time consuming and frustrating. Today all of a sudden I thought of grepping the history command and aahaa!!!!


simply grep the history command as under:

# history | grep perl
235 perl -e "s/rakesh/root/g;" -pi $(find . -type f)
267 perl -e "s/rakesh/root/g;" -pi $(find . -type f)


then you can just type !267 to execute the command or just copy/paste it.

try it and you will save a lot of time for sure.

Thanks

--Rakesh

Nifty Batch Rename files without a loop

All of us know that files can be batch renamed using a for loop as under:
for file in *.xml; do mv $file `echo $file | sed 's/xml/html/'`; done


but Kapil Raj on Unix forum suggested a nifty way to do it without a loop

ls -1 *.xml | nawk -F ".xml" '{print "mv ", $1".xml",$1}' | ksh


Explanation:
ls -1 ---- lists the files vertically
nawk -F ".xml" '{print "mv ", $1".xml",$1}' ---- gets the input from ls -1 and treats .xml as the delimiter. So, if the file name is abc.xml.pqr then $1 would have abc and $2 would have pqr. Also awk prepares the input for a shell (ksh) in the format mv abc.xml.pqr abc.pqr

ksh ---- receives input from nawk and executes mv abc.xml.pqr abc.pqr (renames)



Note: This process is not recursive.


Recursiveness can be obtained by using unix find command.

Ideas and comments are welcome