Find

From Ezee.co.uk

Jump to: navigation, search

The find command is an extremely useful command and one which every sysadmin should have at least a passing familiarity with. It can be used to find a file then execute a command on it plus much more.

find /home/httpd/vhosts/*/statistics/logs -name 'xferlog_regular*' -print -exec rm -fr {} \;

Here are some examples of using the find command

This one will find a file called filename

find / -name 'filename'

This one will find any file that starts with filename

find / -name 'filename*'

This one will find any file with an extension of .pdf

find / -name '*.pdf'

Now if you want to look for something within a set of files, for example looking for a specific ip address in a set of log files.

find /home/httpd/vhosts/*/statistics/logs/ -name 'access_log*' -print -exec grep "195.93.21.100" {} \;

Or for a specific date across the same log file

find /home/httpd/vhosts/*/statistics/logs/ -name 'access_log*' -print -exec grep "08\/Sep\/2005:04:44:" {} \;

If you want to delete all logs that have been compressed and are over a certain age.
Older than 7 days

find /home/httpd/vhosts/*/statistics/logs/ -ctime +7 -name '*.gz' -print -exec rm {} \;  

Remove Sess_ files older than 12 hours

find /tmp -name 'sess_*' -cmin +720 -print -exec rm {} \;

Handy one for plesk admins. This compresses all log files

find /home/httpd/vhosts/ -name 'access_log.processed' -print -exec gzip {} \;

This is a nasty one. It deletes any file that contains a specific string;

find /root/mqueue.bak -name 'qf*' -exec echo grep -i 'Word.Document' {} \> /dev/null \&\& echo {} \; | sh | awk '{s=$0;sub("qf", "df", s); print "rm " $0 " " s;}' | sh

I got this from http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/Q_22787383.html and I am sure there must be an easier way to do it but didn't have time when I needed this.


[edit] SECTION HEADER

Here is some more stuff

Personal tools