Find Files in Linux with find Command Examples
The Linux find command searches for files and directories in a directory hierarchy based on criteria like name, type, size, and modification time, and then can perform actions on the results. You give it a starting directory, a test to apply, and an action to take, and it returns a list of matching paths. This article explains the command’s syntax and provides practical examples you can adapt immediately.
Why the find Command Matters
Every Linux system stores thousands of files across multiple directories. Graphical file managers help, but they are slow and cannot handle complex queries. The find command gives you precise, scriptable control: you can locate a file by name even if you do not know its exact location, find all files larger than a certain size, or delete temporary files older than 30 days. It is one of the most powerful tools in the Unix toolbox because it combines searching with action—you can search and then copy, move, delete, or execute a command on the results.
Unlike the locate command, which uses a pre-built database and can be faster but is often outdated, find scans the actual filesystem in real time. This makes it accurate and reliable, especially for recent changes or on systems where the locate database is not updated regularly.
Basic Syntax and How It Works
The general form of the command is:
find [starting-point...] [expression]
The starting point is the directory where the search begins—for example, . for the current directory, /home/username, or / for the entire system. The expression contains tests (criteria) and actions. If no expression is given, find prints all files and directories under the starting point, which is rarely useful.
Key tests you will use daily:
-name pattern– matches file or directory names (case-sensitive). Use-inamefor case-insensitive matching.-type f– matches regular files;-type dfor directories;-type lfor symbolic links.-size +100M– matches files larger than 100 MB; use-100kfor smaller than 100 KB, andcfor bytes.-mtime -7– matches files modified in the last 7 days;+30means more than 30 days ago.-user username– matches files owned by a specific user.
Actions include -print (the default), -delete, -exec command {} \; to run a command on each file, and -ls for a detailed listing.
Practical Examples for Everyday Use
Here are common tasks solved with find. Replace the paths and patterns with your own.
- Find a file by name in the current directory tree:
find . -name "report.pdf"
This prints the path toreport.pdfif it exists anywhere below the current directory. - Find all JPEG images in your home folder:
find /home/username -type f -name "*.jpg"
The-type fensures you only get files, not directories named like images. - Find empty files and directories:
find . -empty
Useful for cleaning up placeholder files. - Find files larger than 500 MB:
find /var -type f -size +500M
Great for spotting disk hogs. - Find files modified in the last 2 days:
find . -type f -mtime -2
Useful for seeing what changed recently. - Delete all
.tmpfiles in /tmp:find /tmp -type f -name "*.tmp" -delete
Be careful: this permanently removes files. Test without-deletefirst. - Copy all PDFs to a backup folder:
find . -type f -name "*.pdf" -exec cp {} /backup/ \;
The{}is replaced by each file path, and\;ends the command.
How to Choose the Right Options
The key to effective find usage is knowing which test matches your need. Start with the most specific test to narrow results quickly. For example, if you know the file is a document, use -type f; if you know the approximate size, use -size. Combine tests with logical operators: -and (default), -or, and -not. For instance, to find either .png or .jpg files, use:
find . -type f \( -name "*.png" -o -name "*.jpg" \)
The parentheses must be escaped with backslashes to prevent the shell from interpreting them.
When you need to run a command on each result, prefer -exec ... \; for clarity, but note that it spawns a new process for each file, which can be slow for thousands of files. For better performance, use -exec ... + which batches the files into as few command invocations as possible. Example:
find . -type f -name "*.log" -exec grep -l "ERROR" {} +
This searches all matching log files for the string “ERROR” and prints the filenames.
Common Mistakes and Pitfalls
Beginners often forget to specify the starting directory, which defaults to the current directory—this may not be what you intend. Another frequent error is using -name with a pattern that includes a path; -name matches only the basename, not the full path. Use -path to match against the entire path.
Quoting is critical: always quote patterns that contain wildcards (*, ?) so the shell does not expand them before find sees them. For example, find . -name *.jpg will fail if there are multiple .jpg files in the current directory because the shell expands *.jpg into a list of filenames, breaking the command.
Another pitfall is using -delete without testing. Always run the command without -delete first to see what will be removed. Also, be aware that -delete implies -depth, which processes subdirectories before their parents—this is usually what you want, but it can surprise if you expect a different order.
Finally, permission errors are common when searching system directories. You can suppress them by redirecting stderr: find / -name "passwd" 2>/dev/null. This hides error messages but does not affect the results.
Performance, Safety, and Alternatives
Searching the entire filesystem (find /) can be slow and may cause high I/O. Limit the starting directory to the most likely location, or use -prune to exclude directories you know are irrelevant. For example, to skip the /proc directory:
find / -path /proc -prune -o -type f -name "*.conf" -print
For safety, always test with -print before running destructive actions like -delete or -exec rm. Use -ok instead of -exec to be prompted before each action—this is a good habit for critical files.
Alternatives to find include locate (fast but database-based), grep -R for searching file contents, and the fd command (a user-friendly, faster alternative that is not installed by default on most distributions). For simple name searches, locate is often quicker, but find is more flexible and always available.
Frequently Asked Questions
How do I find files by extension in Linux?
Use the -name test with a wildcard: find . -type f -name "*.pdf". The quotes prevent shell expansion. For case-insensitive, use -iname.
How do I find files modified in the last 24 hours?
Use -mtime -1 for files modified less than 1 day ago. For a precise 24-hour window, use -mmin -1440 (1440 minutes in a day).
How do I find and delete files older than 30 days?
Run find /path -type f -mtime +30 -delete. First, run it without -delete to list the files. Be cautious: this permanently removes them.
How do I find files by size in Linux?
Use -size with a suffix: c for bytes, k for kilobytes, M for megabytes, G for gigabytes. For example, find . -type f -size +100M finds files larger than 100 MB.
How do I find files containing a specific text string?
Combine find with grep: find . -type f -exec grep -l "search text" {} \;. The -l option makes grep print only filenames. For faster results on large trees, use grep -R --include="*.txt" "search text" . instead.
Why does my find command return “Permission denied”?
You are trying to search a directory you do not have read permission for, often system directories. Run with sudo if you need to access those, or redirect errors to /dev/null with 2>/dev/null to hide them.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.