-------------------------------------------------------------------------------- find find files (and execute action) -------------------------------------------------------------------------------- The syntax of find is quite different from that of any other unix utility. Summary: find is a powerful tools with rich features but arcane grammar. http://www.linuxnix.com/learn-linuxunix-find-command-50-practical-examples-part-i/ http://www.linuxnix.com/learn-linuxunix-find-command-60-practical-examples-part-ii/ find can refine searches for files based on the following file attributes: (1) file name, (2) file type, (3) file permissions, (4) file owner, (5) time stamp of last modification, (6) last access time stamp and (6) file size. Next, various boolean combinations (AND, OR, Negation) of the above attributes can be used. Newer versions of find allow the use of regular expressions. Finally, the ouptut of find (i.e. list of file names) can be passed to an "exec" module which acts like xargs and thus allow for all sorts of further action on the list of files. Default behavior: search recursively return names of files when an option has a number + .. more than - .. less than " " .. exactly equal Useful switches: -size for files with sizes -size 0 (empty file) -size +1k (files with > 1kByte) -perm +111 (permission, executable files) -type [bcdfglps] type of file (d=directory, f=regular file) -ls #equivalent to ls -l (for file details) -delete #delete all files that satisfy filters! $ file . -type d #show only directories (very useful) ------------------------------------------------------------------------ I. Simple Usage ------------------------------------------------------------------------ $ find SRKUnix -name ls.txt # find a specific file SRKUnix/Tools/ls.txt #note the full filename is returned $ find SRKUnix -iname ls.txt #case insensitive search We are used to thinking "*" as regular expression cf $ ls -R SRKUNix *.c #does a recursive search for files with extension "c" In contrast find intereprets the search parameter literally. If you wish to interpret it as a regular expression you must quote it. $ find SRKUnix -name "*.txt" #find all files with extension c $ find SRKUnix -type d #find file with type "directory" $ find SRKUnix -type f #search for regular files (do not list directories) $ find . -type f -perm +111 #find all executable files A BUG: $ find SRKUNix/ -name "*.html" SRKUnix//Examples.html #note "//"! SRKUnix//UNIXTools.html ------------------------------------------------------------------------ Ib. Multiple directories, multiple files and complementary searches ------------------------------------------------------------------------ $ find SRKUnix UNix -name "*.txt" $ find SRKUnix -name "*.html" -o -name "*.swp" #either .html or .swp $ find SRKUnix \( -name "*.html" -o -name "*.swp" \) #same ^ ^ #mandatory blanks $ find SRKUnix ! \( -name "*.html" -o -name "*.swp" \) #neither $ find SRKUnix ! -name "*.txt" #find all fines which NOT *.txt $ find SRKUnix -not -name "*.txt" #same as previous $ find SRKUnix -type f ! -name "*.txt" #exclude directories as well ------------------------------------------------------------------------ Ic. Exclude directories (useful when searching for pptx files!) ------------------------------------------------------------------------ $ find . -type d \( -path ./Library \) -prune -o -name "*ppt" -print $ find . -type d \( -path ./Library -o -path ./Pictures \) -prune -o -name '*ppt*' -print $ find . -type d \( -path ./Library -o -path ./Pictures -o -path ./.Trash \) -prune -o -mtime -90 -name "*.pptx" -print #search restricted to files modified in the last 90 days ------------------------------------------------------------------------ Id. Search using temporal filters ------------------------------------------------------------------------ $ find SRKUnix -mtime -1 #find files modified in the last day (==24hr) $ find SRKUnix -mtime +1 #find files not modified in the last day #find files which are modifed between 10 and 20 days #notice that options are ANDed $ find SRKUnix -mtime +10 -mtime -20 $ find SRKUnix -mmin -60 #find files modified <60 minutes $ find SRKUnix -mmin +60 #find files modified >60 minutes $ find SRKUnix -mmin +5 -mmin -25 #files modified between 5 and 30 minutes $ find SRKUnix -atime -1 #find files accessed over the last day $ find SRKUnix -cmin 30 #find all files which changed in the last 30 mins $ find SRKUnix -amin 30 #find files accessed more than 30 mins back $ find SRKUnix -newer test.txt #find files created later than that of test.txt ------------------------------------------------------------------------ Ie. Search on sizes of files ------------------------------------------------------------------------ $ find SRKUnix -size 100M #files with size >10 100 Mbytes $ find SRKUnix -size 100M -size 1G #files with sizes [100MB, 1GB] #files of zero length $ find SRKUnix -size 0 $ find . -empty -ls #find and print full details of zero length $ find . -empty -ls -delete #delete! ------------------------------------------------------------------------ II. Find files and then execute ------------------------------------------------------------------------ #find lists the files with pathname starting at the search level #list only basenames #solution: get filenames and use basenames $ find SRKUnix -name "*.txt" -exec basename {} \; #find files which contain a specific phrase $ find SRKUnix -name "*.txt" -exec grep -l "Kitty" {} \; # "{}" is a placeholder for list of filenames # the command must end with "\;" #you could achieve the same result by $ find SRKUnix -name "*.txt" | xargs grep -l "Kitty" #find all files which do not contain Kitty $ find SRKUnix -name "*.txt" | xargs grep -L "Kitty" $ find SRKUnix -type f -name "*.txt" -exec gsed -i 's/Kitty/Mitty/g' {} \; #replace all occurrences of Kitty with Mitty (uh!) $ find SRKUnix -type f -name "*.txt" | xargs tar cvf myfile.tar ------------------------------------------------------------------------ IIa. Multiple executions!! ------------------------------------------------------------------------ The general form is find SRKUnix -type f -name "*.txt" -exec ACTION1 \; ACTION2 \; #find all regular files with .txt extension, list them and see which #have Kitty in them $ find Tools -type f -name "*.txt" -exec ls -l {} \; -exec grep "Kitty" {} \;