I originally created and posted these online on March 8, 2000.
- To recursively remove all files with a given name starting at the current directory, e.g., files called undo.Z, use
find . -name undo.Z -exec rm {} \; |
- To recursively move to the directory ~/public_html/cgi-bin/ all files belonging to user brezeald, use
find . -user brezeald -exec mv {} ~/public_html/cgi-bin/ \; |
- To recursively find all files with the name hosts starting at the current directory, use
find . -name hosts -print |
- To recursively find all files with the extension .htm or .html starting at the current directory, use
find . -name "*.htm*" -print |
- To recursively find all files that don’t begin with a capital letter, use
find . \! -name ‘[A-Z]*’ -print |
- Find all files, but trim out the relative location information
find . -exec basename {} \;
|
- To search for the_word in the current directory and all subdirectories of the current directory, use
find . -type f -exec grep ‘the_word’ {} \; |
- To also print filenames when searching for the_word in the current directory and all subdirectories of the current directory, use
find . -type f -exec grep ‘the_word’ {} \; -print
find . -type f -print | xargs grep the_word 2>/dev/null
|
- To only see the file names, use the -l option for grep:
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.
- To generate a list of all directories, use
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
- Using the GNU version of find, you can limit the depth of the search:
find . -type d -maxdepth 4 -print > /tmp/list.txt |
- To find files that are seven days old, use either of the following (-mtime
is the last modified time of the file):
find . -mtime 7 -print
find . -mtime +6 -mtime -8 -print |
- To find files that have not been accessed in seven days, use
- To find files or directories if only part of the name is known, e.g., if the directory name contains the word REMOVED, use
find . -name \*REMOVED\* -print |
- To set the group id bit on all directories, use
sudo find . -type d -exec chmod g+ws {} \; |
- 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/):
find /somefilesys -follow -mount -type f -print | xargs ls -l |\
sort -r -n -k 5,5 | head -10 |
- A version for files with spaces in the names
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 |
- Would you like to find file names that contain spaces? Try the following:
find . -type f -print | while read TheFileName; do echo "${TheFileName}"
| awk ‘$NF >= 2’ ; done |