Wednesday, 26 December 2007

Useful unix command

As a IT professional, very often, I have to use unix command. Here is list of useful command for easy reference.

#Recursive grep the word
find . -name "*.java" | xargs grep -l <search_word>

#Recursive grep the word ignore case
find . | xargs grep -li <search_word>

#Create tar
tar cvf <file name> <dir>

#List tar
tar tvf <file name>

#Extract tar
tar xvf <file name>

#Zip file - Original file changed
gzip <files>

#Zip file - Original file unchanged
gzip -c <files> > <tar file>

#Unzip the file
gunzip test.tar.gz

#Use awk to tokenise string
cat file.txt | awk -F ":" '{print $2}'

#Grep file name to copy to another directory
grep -l '\[273\]' *.txt | xargs -i cp {} <dir>

#Sort lines in file
sort <file>.txt >> <file>.txt.so

#Remove duplicate
sort <file>.txt | uniq > <file>.sort

#Remove spaces
cat <file>.txt | sed -e 's/ //g'

#Find files bigger than this size 1.5MB
find . -type f -size +1572864c -exec ls -l {} \;

#Show space occupied by directory and content
du -ks <directory>

#Sum and sort the space occupied by each sub directory
du -ks ./* | sort -n

#Remove old log files
find . -type f -mtime +45 -exec rm -f {} \;

#Find big old files
find . -type f -mtime +365 -size +700000c -exec ls -l {} \; >> big_old_files.txt 2>&1 &

0 comments:

Design by Dzelque Blogger Templates 2007-2008