Linux - Find Command

basic 'find file' commands

1find / -name foo.txt -type f -print             # full command
2find / -name foo.txt -type f                    # -print isn't necessary
3find / -name foo.txt                            # don't have to specify "type==file"
4find . -name foo.txt                            # search under the current dir
5find . -name "foo.*"                            # wildcard
6find . -name "*.txt"                            # wildcard
7find /users/al -name Cookbook -type d           # search '/users/al'

search multiple dirs

1find /opt /usr /var -name foo.scala -type f     # search multiple dirs

case-insensitive searching

1find . -iname foo                               # find foo, Foo, FOo, FOO, etc.
2find . -iname foo -type d                       # same thing, but only dirs
3find . -iname foo -type f                       # same thing, but only files

find files with different extensions

1find . -type f \( -name "*.c" -o -name "*.sh" \)                       # *.c and *.sh files
2find . -type f \( -name "*cache" -o -name "*xml" -o -name "*html" \)   # three patterns

find files that don't match a pattern (-not)

1find . -type f -not -name "*.html"  # find all files not ending in ".html"

find files by text in the file (find + grep)

1find . -type f -name "*.java" -exec grep -l StringBuffer {} \; # find StringBuffer in all *.java files
2find . -type f -name "*.java" -exec grep -il string {} \;      # ignore case with -i option
3find . -type f -name "*.gz" -exec zgrep 'GET /foo' {} \;       # search for a string in gzip'd files

5 lines before, 10 lines after grep matches

1find . -type f -name "*.scala" -exec grep -B5 -A10 'null' {} \;

find files and act on them (find + exec)

1find /usr/local -name "*.html" -type f -exec chmod 644 {} \;      # change html files to mode 644
2find htdocs cgi-bin -name "*.cgi" -type f -exec chmod 755 {} \;   # change cgi files to mode 755
3find . -name "*.pl" -exec ls -ld {} \;                            # run ls command on files found

find and copy

1find . -type f -name "*.mp3" -exec cp {} /tmp/MusicFiles \;  # cp *.mp3 files to /tmp/MusicFiles

copy one file to many dirs

1find dir1 dir2 dir3 dir4 -type d -exec cp header.shtml {} \;  # copy the file header.shtml to those dirs

find and delete

1find . -type f -name "Foo*" -exec rm {} \;  # remove all "Foo*" files under current dir
2find . -type d -name CVS -exec rm -r {} \;  # remove all subdirectories named "CVS" under current dir

find files by file permission

1find . -type f -perm 0777 -print
2find / -type f ! -perm 777

find files by modification time

1find / -mtime 1  # Find Last 24 hours Modified Files	
2find / -mtime 50 # Find Last 50 Days Modified Files	
3find / -atime 50 # Find Last 50 Days Accessed Files	
4find / -mtime +50 –mtime -100 # Find Last 50-100 Days Modified Files	
5find / -cmin -60 # Find Changed Files in Last 1 Hour	
6find / -mmin -60 # Find Modified Files in Last 1 Hour	
7find / -mtime -50 -type f  # Find Last 50 Days Modified Files only
8find / -mtime -7 -type d   # Find Last 50 Days Modified Dir only

find files by file size

1find / -size 50M # Find 50MB Files	
2find / -size +50M -size -100M # Find Size between 50MB – 100MB
3find / -type f -name *.mp3 -size +10M -exec rm {} \; # Find Specific Files and Delete	

find files by modification time using a temp file

1touch 09301330 poop           # 1) create a temp file with a specific timestamp
2find . -mnewer poop           # 2) returns a list of new files
3rm poop                       # 3) rm the temp file

find with time: this works on mac os x

1find / -newerct '1 minute ago' -print

find and tar

1find . -type f -name "*.java" | xargs tar cvf myfile.tar
2find . -type f -name "*.java" | xargs tar rvf myfile.tar

find, tar, and xargs

1find . -name -type f '*.mp3' -mtime -180 -print0 | xargs -0 tar rvf music.tar

-print0 helps handle spaces in filenames

find and pax (instead of xargs and tar)

1find . -type f -name "*html" | xargs tar cvf jw-htmlfiles.tar -
2find . -type f -name "*html" | pax -w -f jw-htmlfiles.tar

locate command

1locate tomcat.sh          # search the entire filesystem for 'tomcat.sh' (uses the locate database)
2locate -i spring.jar      # case-insensitive search