Cheat sheets for Find
Cheat sheets for Find
#General
Usage
find <path> <conditions> <actions>
Conditions
-name "*.c"
-user jonathan
-nouser
# File
-type f
# Directory
-type d
# Symlink
-type l
# At least 3 levels deep
-depth 2
-regex PATTERN
# Exactly 8 512-bit blocks
-size 8
# Smaller than 128 bytes
-size -128c
# Exactly 1440KiB
-size 1440k
# Larger than 10MiB
-size +10M
# Larger than 2GiB
-size +2G
-newer file.txt
# modified newer than file.txt
-newerm file.txt
# [c]hange, [m]odified, [B]create
-newerX file.txt
# [t]imestamp
-newerXt "1 hour ago"
Basic 'find file' commands
# full command
find / -name foo.txt -type f -print
# -print isn't necessary
find / -name foo.txt -type f
# don't have to specify "type==file"
find / -name foo.txt
# search under the current dir
find . -name foo.txt
# wildcard
find . -name "foo.*"
# wildcard
find . -name "*.txt"
# search '/users/al'
find /users/al -name Cookbook -type d
Search multiple dirs
# search multiple dirs
find /opt /usr /var -name foo.scala -type f
Case-insensitive searching
# find foo, Foo, FOo, FOO, etc.
find . -iname foo
# same thing, but only dirs
find . -iname foo -type d
# same thing, but only files
find . -iname foo -type f
Find files with different extensions
# *.c and *.sh files
find . -type f \( -name "*.c" -o -name "*.sh" \)
# three patterns
find . -type f \( -name "*cache" -o -name "*xml" -o -name "*html" \)
Find files that don't match a pattern (-not)
# find all files not ending in ".html"
find . -type f -not -name "*.html"
Find files by text in the file (find + grep)
# find StringBuffer in all *.java files
find . -type f -name "*.java" -exec grep -l StringBuffer {} \;
# ignore case with -i option
find . -type f -name "*.java" -exec grep -il string {} \;
# search for a string in gzip'd files
find . -type f -name "*.gz" -exec zgrep 'GET /foo' {} \;
5 lines before, 10 lines after grep matches
find . -type f -name "*.scala" -exec grep -B5 -A10 'null' {} \;
Find files and act on them (find + exec)
# change html files to mode 644
find /usr/local -name "*.html" -type f -exec chmod 644 {} \;
# change cgi files to mode 755
find htdocs cgi-bin -name "*.cgi" -type f -exec chmod 755 {} \;
# run ls command on files found
find . -name "*.pl" -exec ls -ld {} \;
Find and copy
# cp *.mp3 files to /tmp/MusicFiles
find . -type f -name "*.mp3" -exec cp {} /tmp/MusicFiles \;
Copy one file to many dirs
# copy the file header.shtml to those dirs
find dir1 dir2 dir3 dir4 -type d -exec cp header.shtml {} \;
Find and delete
# remove all "Foo*" files under current dir
find . -type f -name "Foo*" -exec rm {} \;
# remove all subdirectories named "CVS" under current dir
find . -type d -name CVS -exec rm -r {} \;
Access time conditions
-atime 0 # Last accessed between now and 24 hours ago
-atime +0 # Accessed more than 24 hours ago
-atime 1 # Accessed between 24 and 48 hours ago
-atime +1 # Accessed more than 48 hours ago
-atime -1 # Accessed less than 24 hours ago (same a 0)
-ctime -6h30m # File status changed within the last 6 hours and 30 minutes
-mtime +1w # Last modified more than 1 week ago
These conditions only work in MacOS and BSD-like systems (no GNU/Linux support).
Find files by modification time
# 24 hours
find . -mtime 1
# last 7 days
find . -mtime -7
# just files
find . -mtime -7 -type f
# just dirs
find . -mtime -7 -type d
Find files by modification time using a temp file
# 1) create a temp file with a specific timestamp
touch 09301330 poop
# 2) returns a list of new files
find . -mnewer poop
# 3) rm the temp file
rm poop
Find with time: this works on mac os x
find / -newerct '1 minute ago' -print
Find and tar
find . -type f -name "*.java" | xargs tar cvf myfile.tar
find . -type f -name "*.java" | xargs tar rvf myfile.tar
Find, tar, and xargs
find . -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)
find . -type f -name "*html" | xargs tar cvf jw-htmlfiles.tar -
find . -type f -name "*html" | pax -w -f jw-htmlfiles.tar