Wednesday, June 27, 2012

Find Examples

Share it Please

The examples were used by me mostly during my work, (continuously updated)

Find and Gzip

find $PWD –type f –name “*.log” | xargs tar zcvf one.tar

Find and Remove

find –type f –name “*.log” | xargs rm
find –type f –name “*.log” -exec rm -f {} \;

Find and get confirmation to remove

find –type f –name “*.log” –ok rm {} ‘;’

Find files with size and compress

find $PWD –size + 10000c –type f –name “*.log”  | xargs tar zcvf one.tar

Find files with size and compress , that are not already compressed by gzip

find $PWD –size + 10000c –type f –name “*.log”  -regex ‘.*[^gz]’ –exec gzip ‘{}’ ‘;’

Find and grep (grep is)

find $PWD –size + 10000c –type f –name “*.log” –exec grep is {} ‘;’

List the number of files in the given directory,

Using a special predicate ‘\! –type d|f’ to exclude the listing of directories or files

Find $pwd ‘! –type d | wc –l ( list only files)

Find $pwd ‘! –type f | wc –l ( list only directories)

Find using max depth

Find $PWD –maxdepth 1 –type d –print

Find and stop executing if the result is blank

Find $PWD –name “*.bak” –print | xargs –r ls –l

If the result of find is blank ,the ls –l will not get   executed if we use “-r” option 

Redirect errors with  out seeing them on screen

find -name "*.txt" 2>>/dev/null

Find files that are accessed after the /etc/hosts/ was modified

find -anewer /etc/hosts

Find all files which are modified after the /sample file is modified

find -newer $PWD/sample

Find Files Which Changed in The Last 60 minutes

find $PWD  -cmin -60

Find Files Which Accessed in The Last 60 minutes

find  $PWD  -amin -60

FInd Files That are Updated in The Last 60 Minutes

find $PWD -mmin -60

Find all files Older than 2 days and gzip them

find $PWD -type f -mtime +2 | xargs gzip

Find the Top 20 high size files

find $PWD -type f -print0 | xargs -0 du -h | sort -nr | head -20

Find Broken Sym Links

find -type l ! -exec test -e {} \;

Find Empty Files

Find $PWD -empty
Find and Move Files
find /home -iname '*.zip' -exec mv '{}' /backup/ \;
Grep a File
find $PWD -type f | grep PAVAN
find $PWD -type f -exec egrep PAVAN {} \;

Find and Delete a file

find $PWD -type f –delete
find /tmp/bar/ -type f -delete

Please Let me Know if these need any modifications.

Happy Learning J