Monday, September 7, 2009

Linux commands

Here are the series of commands bash tip/tricks which I have learn time to time while working.

# To report oldest file in a directory
ls -1rt | head -n1

# To rename lot of files to different name
for i in `find -type d`; do mv $i `basename $i (ska)`; done

# Very handy while copying ssh public key to remote server, where one have to create .ssh directory, authorized file, keeping the permissions straight.
mkdir .ssh --mode 700 ; touch .ssh/authorized_keys ; chmod 644 .ssh/authorized_keys
# Whats the quick way to remove .blah.com from mycomany.blah.com ?

$str=~s/\.blah\.com//i

# Finding files and then deleting them

find /software/ftp/ -name '*.exe' -print0 | xargs -0 rm -f

Or much better to use the -exec action:

find /software/ftp/ -name '*.exe' -exec rm {} \;

# Extract url for path to Full version of last kernel for each branch

wget -O /dev/stdout http://www.kernel.org/ 2>/dev/null | grep ">F<"|head |awk -F "\"" '{ print $2 }' # This version will download the kernels wget -O /dev/stdout http://www.kernel.org/ 2>/dev/null | grep ">F<"|head |awk -F "\"" '{ print $2 }'|while read line;do if [ -n "$line" ]; then wget -c "http://www.kernel.org$line";fi;done#

#Find files that has been access +n days ago and then remove it.

find . -mtime +180 -exec ls -lh {} \;
find . -mtime +180 -exec rm -f {} \;

No comments:

Post a Comment