Based on regular expression
. : dot is CWD (current working directory)
find . -print : basic-usage
find . -name [filename] : search based on file name
find . -iname [filename] : search based on file name but case insensitive
find . -iname '*.txt' : search based on extension
find . -iname 'data*' : search file begin with data
find . \( -name '*.txt' -o -name '*.pdf' \) -print : search with logical operations
find . \( -name '*e*' -and -name 's*' \) : search file start with s and contain e character
find . -regex '.*\.(py\|sh\)$' : find with regex
find . -iregex '.*\.(py\|sh\)$' : find with case insensitive regex
find . -print0 | xargs : -print0 to pass filenames containing white char to the xargs
Based on file type
find . -type f : search for file
find . -typd d : search for directory
find . -type l : search for symbolic link
find . -type c|b|s|p : search for char special dev, block dev, socket, pipo
Negating arguments
find . ! -name "*.txt" -print : find all files exclude .txt
Based on the directory depth
find -L /proc -maxdepth 3 -name 'file.sh'
- L, include symbolic link
- /procs, folder to start searching
- -maxdepth 3, limits the search to only current folder
find . -mindepth 2 -name "f*" -print
- print files begin with f and that are at least two subdir
- distant from the current directory
Based on file timestamp
The find command's timestamp flags are useful for writing backup and maintenance scripts.
There are three types of timestamp :
- Access time (-atime) : Last accesse
- Modification time (-mtime) : Last modified
- Change time (-ctime) : Last change of metadata (permission or ownership)
- Use days as default
find . -type f -atime -7 -print : Last accessed within seven days
find . -type f -atime 7 -print : Last accessed exactly seven days old
find . -type f -atime +7 -print : Last accessed older than seven days old
find . -type f -mtime -7 -print : Last modified within seven days
find . -type f -mtime 7 -print : Last modified exactly seven days old
find . -type f -mtime +7 -print : Last modified older than seven days old
find . -type f -ctime -7 -print : Last change within seven days
find . -type f -ctime 7 -print : Last change exactly seven days old
find . -type f -ctime +7 -print : Last change older than seven days old
find . -type f -amin +7 -print : Last accessed older than seven minutes
find . -type f -mmin +7 -print : Last modified older than seven minutes
find . -type f -cmin +7 -print : Last change older than seven minutes
find . -type f -newer file.txt -print : Find files that were modified more recently than file.txt
Based on file size
b: 512 byte blocks
c: Bytes
w: Two-byte words
k: Kilobytes (1,024 bytes)
M: Megabytes (1,024 kilobytes)
G: Gigabytes (1,024 megabytes)
find . -type f -size +2G : Files having size greater than 2 Gigabytes
find . -type f -size -2G : Files having size less than 2 Gigabytes
find . -type f -size 2G : Files having size 2 Gigabytes
Based on file permissions and ownerships
find . -type f -perm 644 -print : Print files having permission 644
find . -type f -name "*.php" ! -perm 644 -print : Print php files that is not 644
find . -type f -user darin -print : Print files owned by user darin
Performing actions on files with find
You cannot use multiple commands along with the -exec parameter.
It accepts only a single command (you must use shell script).
For example : -exec ./commands.sh {} \;
find . -type f -name "*.swp" -delete : Remove matched .swp file
find . -type f -user root -exec chown darin {} \; : Find files owned by root, change permission to darin
find . -type f -name '*.txt' -exec cat {} \; : Find .txt files and cat
find . -type f -name '*.txt' -exec cat {} > file \; : Find .txt files, read and truncate to file
find . -type f -mtime +10 -name "*.txt" -exec cp {} OLD \;
copy all the .txt files that are older than 10 days to a directory OLD:
find . -type f -name "*.txt" -exec printf "Text file: %s\n" {} \;
The -exec parameter can be coupled with printf to produce joutput.