Title: Ways to use the UNIX command find
Author: Darin Brezeale
Date: March 8, 2000
Updated: Thursday, 19-Jul-2007 23:12:06 EDT
Keywords: find recursive files
Notes:
  1. To recursively remove all files with a given name starting at the current directory, e.g., files called undo.Z, use
  2. find . -name undo.Z -exec rm {} \;
  3. To recursively move to the directory ~/public_html/cgi-bin/ all files belonging to user brezeald, use
  4. find . -user brezeald -exec mv {} ~/public_html/cgi-bin/ \;
  5. To recursively find all files with the name hosts starting at the current directory, use
  6. find . -name hosts -print
  7. To recursively find all files with the extension .htm or .html starting at the current directory, use
  8. find . -name "*.htm*" -print
  9. To recursively find all files that don't begin with a capital letter, use
  10. find . \! -name '[A-Z]*' -print
  11. Find all files, but trim out the relative location information
  12. find . -exec basename {} \;

  13. To search for the_word in the current directory and all subdirectories of the current directory, use
  14. find . -type f -exec grep 'the_word' {} \;
  15. To also print filenames when searching for the_word in the current directory and all subdirectories of the current directory, use
  16. find . -type f -exec grep 'the_word' {} \; -print
    find . -type f -print | xargs grep the_word 2>/dev/null

  17. To only see the file names, use the -l option for grep:
  18. find . -type f -print | xargs grep -l the_word 2>/dev/null

    The " -type f " option for find indicates that the files should be plain, as opposed to links, directories, etc.

  19. To generate a list of all directories, use
  20. find . -type d -local -ls -print > /tmp/list.txt

    -type d -- only directories
    -local -- only local
    -ls -- file statistics (date, size, etc.)
    -print -- display the results

  21. Using the GNU version of find, you can limit the depth of the search:
  22. find . -type d -maxdepth 4 -print > /tmp/list.txt
  23. To find files that are seven days old, use either of the following (-mtime is the last modified time of the file):
  24. find . -mtime 7 -print
    find . -mtime +6 -mtime -8 -print
  25. To find files that have not been accessed in seven days, use
  26. find . -atime 7 -print
  27. To find files or directories if only part of the name is known, e.g., if the directory name contains the word REMOVED, use
  28. find . -name \*REMOVED\* -print
  29. To set the group id bit on all directories, use
  30. sudo find . -type d -exec chmod g+ws {} \;
  31. Try this one-liner to generate a list of the 10 largest files on the target file system (from http://www.unixreview.com/documents/s=8925/ur0310e/):
  32. find /somefilesys -follow -mount -type f -print | xargs ls -l |\
    sort -r -n -k 5,5 | head -10
  33. A version for files with spaces in the names
  34. find /somefilesys -follow -mount -type f -exec find {} \! -type l -print
    \; | perl -ne 'chomp; ($size) = (stat("$_"))[7]; print "$size $_\n";'
    | sort -nr +0 -1 | head -10 | cut -d' ' -f2- | while read TheFile; do
    ls -l "$TheFile" ; done | sort -nr +4 -5
  35. Would you like to find file names that contain spaces? Try the following:
  36. find . -type f -print | while read TheFileName; do echo "${TheFileName}"
    | awk '$NF >= 2' ; done