Wednesday, July 11

"find" and "grep" command


"find" command is used to search for files. you can specify many options with it like files created today or having size greater then you specified. Normally we also combine find with xargs or exec to issue commands on files returned by find.
examples of find command:
* find top 10 largest files in /var:
$ find /var -type f -ls | sort -k 7 -r -n | head -10

* find all files having size more than 5 GB in /var/log/:
$ find /var/log/ -type f -size +5120M -exec ls -lh {} \;
* find all today’s files and copy them to another directory:
$ find /home/me/files -ctime 0  -print -exec cp {} /mnt/backup/{} \;
* find all temp files older than a week and delete:
$ find /temp/ -mtime +7-type f | xargs /bin/rm -f
* find and rename all mp3 files by changing their uppercase names to lowercase:
$ find /home/me/music/ -type f -name *.mp3 -exec rename 'y/[A-Z]/[a-z]/' '{}' \;
some examples of grep command:
* Print Apache’s documentroot directory name:
$ grep -i documentroot  /etc/httpd/conf/httpd.conf
* View file contents without comments and empty lines:
$ grep -Ev “^$|^#” /etc/my.cnf
* print only IP address assigned to the interface:
$ ifconfig eth0 | grep 'inet addr:' | cut -d':' -f2 | awk '{ print $1}'
* How many email messages sent for a particular date:
$ cat /var/log/maillog | grep "status=sent" | grep "July 11" | wc -l
* Find out a running process/daemon from process list (thanks to staranneph for recalling this):
ps -ef | grep mysql
***********************************************************************
Thanks




No comments:

Post a Comment