Linux find Command: Search for Files
find searches a directory, and everything inside it, for files and folders that match what you ask for: a name, a size, a date and more. It can then run a command on everything it finds, which makes it one of the most useful tools on the Linux command line.
How a find command is built
find [where to look] [what to match] [what to do]For example, find /home -name "*.pdf" looks in /home for files ending in .pdf. Use . to search the current directory. If you leave out the action, find simply lists what it finds.
Search by name
find . -name "report.txt"Finds files called report.txt in the current directory and below.
find . -iname "*.jpg"Finds files ending in .jpg, ignoring upper and lower case, so photo.JPG is found too. Put patterns in quotes so the shell doesn't expand the * first.
find . -type d -name "backup*"Finds only directories (-type d) whose names start with backup. Use -type f for files only.
find / -name "test*" 2>/dev/nullSearches the whole system for anything starting with test. Adding 2>/dev/null hides the "Permission denied" messages for folders you can't read. Put sudo in front to search everywhere.
Search by size and date
find . -type f -size +100MFinds files bigger than 100 MB. Handy when a disk is filling up. Use - for smaller than (-size -10k). A size with no + or - matches files of about that exact size.
find . -type f -mtime -7Finds files changed in the last 7 days. -mtime +30 finds files not changed for more than 30 days.
find . -type f -mmin -60Finds files changed in the last 60 minutes.
find . -type f -emptyFinds empty files.
Do something with the results
find . -name "*.log" -exec grep -l "error" {} +
Searches inside every .log file and lists the ones that contain the word error. The {} is
replaced by the files that were found.
find . -type f -name "*.tmp" -mtime +30 -deleteDeletes .tmp files that haven't been changed for more than 30 days.
Check before you delete. Always run the command without -delete first and check the list of files. There's no recycle bin, and -delete removes everything that matches.
Advanced examples
find . -type f -printf '%TY-%Tm-%Td\t%TH:%TM:%.2TS\t%P\n' \ | sort -n \ | cut -f3Lists every file in the current directory and below, sorted by the time it was last changed, with the most recent last.
find . -type f -size +2M -exec file -N --mime-type {} + \
| grep ': image/' \
| cut -d: -f1
Finds all images over 2 MB, whatever their name or extension, by checking what type of file each one really
is.
find . -type f -size +2M \( -iname '*.png' -o -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.gif' \)A faster way to find images over 2 MB, by file extension. -o means "or", and the brackets group the choices together. Images with other extensions, such as .bmp, won't be found.
find . -regex '.*/20[0-9][0-9]-07-10-[A-Z][a-z][a-z]-\(0[8-9]\|1[0-7]\)-[0-9][0-9]-[0-9][0-9]\.jpg'Uses a regular expression to find photos taken on 10 July between 8:00 am and 5:59 pm, when the file names look like 2001-07-10-Tue-17-55-01.jpg. A regex has to match the whole path, which is why it starts with .*/.
Quick reference
| Option | Matches |
|---|---|
-name / -iname | File name (-iname ignores case) |
-type f / -type d | Files only / directories only |
-size +100M | Bigger than 100 MB (k, M and G work) |
-mtime -7 | Changed in the last 7 days |
-mmin -60 | Changed in the last 60 minutes |
-empty | Empty files and directories |
-regex | Whole path matches a regular expression |
-o | Or (combine with \( \)) |
-exec cmd {} + | Run a command on the results |
-delete | Delete the results (test first) |