Pages

Tuesday, 11 September 2012

Misc usage of find

Search for file with a specific name in a set of files (-name)
-print wil print out the path of any file
find . -name "rc.conf" -print

How to apply a unix command to a set of file (-exec).
find . -name "rc.conf" -exec chmod o+r '{}' \;


Remove files by mtime, dampend by sleep to avoid impact
x=800
while [ $x -ge 15 ]
do
 echo "removing $x days old data"
find /var/FortiGuard/remove -mtime +$x -print |xargs rm -rf
 sleep 300
 x=$(( $x - 10 ))
done

How to search for a string in a selection of files (-exec grep ...).
find . -exec grep "www.athabasca" '{}' \; -print
This command will search in the current directory and all sub directories. All files that contain the string will have their path printed to standard output.

If you want to just find each file then pass it on for processing use the -q grep option. This finds the first occurrance of the search string. It then signals success to find and find continues searching for more files.

find . -exec grep -q "www.athabasca" '{}' \; -print
This command is very important for process a series of files that contain a specific string. You can then process each file appropriately. An example is find all html files with the string "www.athabascau.ca". You can then process the files with a sed script to change those occurrances of "www.athabascau.ca" with "intra.athabascau.ca".

for size in `find . -type f -newer 1239832944:7 -exec ls -ltr '{}' \; | cut -d ' ' -f6`; do let total=$total+$size; done

//touch a wosit with a particular modified date
touch -t 201205120000 /tmp/wosit
//find a file having a newer modified date than /tmp/wosit
find ./ -newer /tmp/wosit
find -type f -size +5000k -exec ls -lh {} \; <-- if 2 para, {} {}



No comments:

Post a Comment