The Linux find command is regarded as one of the most effective and comprehensive tools that can be used to find files and directories in the filesystem. Being a system administrator, you need to maintain the servers, or if you are a developer, you have to arrange the files of your project. Then, the find command is one command that you can learn, and this command can save your productivity and help you move through complicated directory structures easily.
What is the ‘find’ Command?
The find command refers to a Linux command that is used to search files and folders within a directory. You can find information using such attributes as name, size, type, permissions, or one of the last changed times of the file. Find is able to execute actions on files it finds, unlike simple commands that simply list files; find also enters subfolders and can even execute actions on them.
Basic Syntax of the Find Command
The basic syntax of the find command follows the pattern below:
# Find files using custom path and expression
find [path] [expression]
- path: The starting directory for the search (defaults to the current directory if omitted)
- expression: Criteria for matching files and actions to perform
How the Find Command Works in Linux
The Linux find command is used to scan folders and files on the fly. Find is also an operation that views the current files and folders at the moment, whereas the locate command will use an old database, which may not reflect the recent changes. In such a manner, it is also able to locate files that were recently modified or newly created.
This is how the process works:
- Starting Point – The command begins from the directory you specify (for example, /home or . for the current folder). From this specific point, it starts to explore all files and subdirectories.
- Applying Conditions – You can define rules such as file name, type, size, permissions, or modification time. These rules are compared with every file or folder.
- Using Options –With Additional Options. The additional options allow refining the search, such as limiting the depth or not honoring case sensitivity.
- Taking Action – By default, find lists the matching file paths. However, you can also have it do things, e.g., deleting files, moving them, or even running some custom command with -exec.
The find package of the GNU system includes the find command, and it’s already installed in most Linux distributions, so you can immediately use it.
Essential Examples and Use Cases
1. Finding Files by Name
The find command can search for files by their names. It enables you to look for an exact file, search with patterns, or ignore upper/lower case.
# Find files with exact name
find /home/user -name "document.txt"
# Find files with pattern matching (case-sensitive)
find . -name "*.log"
# Case-insensitive search
find . -iname "*.PDF"
2. Finding Files by Type
Occasionally, don’t want to search for everything and require a specific file type. The -type option in Linux helps with this.
- -type f → Finds only regular files (like .txt, .log, etc.).
- -type d → Finds only directories (folders).
- -type l → Finds symbolic links (shortcuts to other files).
These are some examples:
# Find only regular files
find /var/log -type f
# Find only directories
find /home -type d
# Find symbolic links
find /usr/bin -type l
3. Finding Files by Size
Sometimes you need to find big files that take up space or tiny files you may have forgotten about. The -size option in Linux helps you do that.
- + (plus) → larger than the given size
- – (minus) → smaller than the given size
- No sign → exactly that size
Here are some examples:
# Find files larger than 100MB
find / -size +100M
# Find files smaller than 1KB
find . -size -1k
# Find files exactly 50 bytes
find . -size 50c
Size units include:
- c for bytes
- k for kilobytes
- M for megabytes
- G for gigabytes
4. Finding Files by Modification Time
If you want to know when files were last updated, the -mtime option is very handy. It helps you search by the number of days since a file was changed.
- -7 → files modified within the last 7 days
- +30 → files modified more than 30 days ago
- 1 → files modified exactly 1 day ago
Here are some examples:
# Files modified in the last 7 days
find /home -mtime -7
# Files modified more than 30 days ago
find /tmp -mtime +30
# Files modified exactly 1 day ago
find . -mtime 1
5. Finding Files by Permissions
To get a file based on its individual permission settings, you can use the -perm option. It’s most helpful to system admins who need to check which files are readable, writable, or executable.
Examples to show it are:
# Find files with exact permissions
find . -perm 755
# Find files with at least these permissions
find . -perm -644
# Find executable files
find . -perm /111
Advanced Search Techniques
Combining Multiple Criteria
The command also allows for intelligent searching by mixing conditions. You can narrow down or expand your search using AND, OR, and NOT.
# Find large log files (AND condition)
find /var/log -name "*.log" -size +10M
# Find files that are either .txt OR .doc (OR condition)
find . \( -name "*.txt" -o -name "*.doc" \)
# Find files that are NOT .tmp files (NOT condition)
find . ! -name "*.tmp"
See these simplified explanations:
- AND → The two conditions must apply (e.g., file is .log and bigger than 10MB).
- OR → Either condition can be valid (e.g., file is .txt or .doc).
- NOT → Excludes files (e.g., ignore all .tmp files).
Using Regular Expressions
The -regex option allows you to search for files with regular expressions (regex). Regex enables the matching of patterns in filenames. It provides greater flexibility than when using -name.
# Find files matching a regex pattern
find . -regex ".*\.\(jpg\|png\|gif\)"
# Case-insensitive regex
find . -iregex ".*\.jpe?g$"
Explanation:
- .* → Matches any file path.
- \(jpg\|png\|gif\) → Means “jpg OR png OR gif”.
- -iregex → Ignores case (so .JPG and .jpg are both matched).
It’s a worthy command when you:
- Required to find many file types simultaneously.
- Need more control than -name can give.
Limiting Search Depth
Sometimes admins don’t want their search to go through every folder. The options -maxdepth and -mindepth allow you to control the depth of your search inside directories.Â
# Search only in current directory (depth 1)
find . -maxdepth 1 -name "*.txt"
# Skip the first level and search deeper
find . -mindepth 2 -name "config"
The explanations:
- -maxdepth 1 → Will look only in your current directory.
- -mindepth 2 → Ignores the first level and starts deeper.
This is useful when:
- You just want files in the current folder.
- You don’t want to waste time searching unnecessary subfolders.
Performing Actions on Found Files
The real power of find comes from its ability to execute commands on the files it discovers.
Using -exec
Execute commands on each file found:
# Delete all .tmp files
find . -name "*.tmp" -exec rm {} \;
# Change permissions of all .sh files
find . -name "*.sh" -exec chmod +x {} \;
# Copy the .conf files to a backup directory
find /etc -name "*.conf" -exec cp {} /backup/ \;
A placeholder {} shows individual files found, while \; terminates the command.
Using -exec with Multiple Files
Processes multiple files at once to improve performance:
# Remove multiple files in a single command
find . -name "*.tmp" -exec rm {} +
# Move multiple files
find . -name "*.old" -exec mv {} /archive/ +
Using -delete
The command for promptly deleting files:
# Delete empty directories
find . -type d -empty -delete
# Delete old log files
find /var/log -name "*.log" -mtime +30 -delete
Using -print0 with xargs
The handling of filenames containing spaces or special characters occurs through:
# Safely process files with spaces in names
find . -name "*.txt" -print0 | xargs -0 grep "search term"
# Compress multiple files
find . -name "*.log" -print0 | xargs -0 gzip
Practical Real-World Examples
System Administration Tasks
# Find and remove core dumps older than 7 days
find / -name "core.*" -mtime +7 -delete 2>/dev/null
# Find world-writable files (security audit)
find / -perm -002 -type f 2>/dev/null
# Find SUID files
find / -perm -4000 -type f 2>/dev/null
# Clean up temporary files
find /tmp -type f -atime +7 -delete
Development and File Management
# Find all Python files and count lines of code
find . -name "*.py" -exec wc -l {} + | tail -1
# Find and backup configuration files
find /home -name ".*rc" -exec cp {} /backup/configs/ \;
# Find duplicate filenames
find . -type f -printf "%f\n" | sort | uniq -d
# Find empty files and delete them
find . -empty -type f -delete
# Find empty directories and delete them
find . -empty -type d -delete
Log Analysis and Monitoring
# Find recent error logs
find /var/log -name "*.log" -mtime -1 -exec grep -l "ERROR" {} \;
# Find large log files that need rotation
find /var/log -name "*.log" -size +100M
# Monitor configuration changes
find /etc -name "*.conf" -mtime -1
Performance Hacks and Best Practices
1. Optimize Search Paths
Reduce search time by beginning your search from the most specific directory possible:
# Instead of searching from root
find / -name "httpd.conf"
# Search in likely locations
find /etc /usr/local/etc -name "httpd.conf"
2. Use Early Filters
Take the most restrictive criteria and place them first:
# More efficient: filter by type first
find . -type f -name "*.log" -size +10M
# Less efficient: size check on all entries
find . -size +10M -type f -name "*.log"
3. Suppress Error Messages
Stop your output from becoming cluttered by redirecting stderr:
# Find all .conf files, suppressing permission errors
find / -name "*.conf" 2>/dev/null
4. Use Specific File Types
Specify file types to avoid unnecessary checks:
# Only check regular files
find . -type f -name "pattern"
Common Pitfalls and Troubleshooting
Handling Special Characters
Cases where filenames possess spaces or special characters:
# Use -print0 with xargs -0
find . -name "*.txt" -print0 | xargs -0 ls -l
# Quote the search pattern with spaces
find . -name "file with spaces.txt"
Permission Denied Errors
Suppress or handle permission errors appropriately:
# Suppress all errors
find / -name "*.conf" 2>/dev/null
# Redirect errors to a file for review
find / -name "*.conf" 2>errors.log
Case Sensitivity
Remember that -name is case-sensitive while -iname is not:
# Only matches .PDF (case-sensitive)
find . -name "*.PDF"
# Matches .pdf, .PDF, .Pdf, etc. (case-insensitive)
find . -iname "*.pdf"
Alternative Tools and When to Use Them
Although find is incredibly useful, sometimes alternative tools prove to be more appropriate for the use:
- locate: Faster for simple name searches using a pre-built database
- grep -r: Better for searching file contents
- fd: A modern, faster alternative to find with simpler syntax
- tree: Better for visualizing directory structures
Also Read
Rename file linux
AWS CodeWhisperer: an AI-powered coding companion
Conclusion
Linux find is a tool that cannot be done without by any person dealing with Linux-based systems. From simple file searches to complex system administration, master find and become more efficient and able to manage complex file management situations.
Begin with simple searches and add additional functionality as you grow familiar with it. Always remember to check these commands in a safe environment before executing them on any important systems, particularly when you have to use such options as – delete or -exec rm.
The trick to finding is to know how to use its multiple options to make specific search terms and then do something significant with the findings. With experience, you will realize that complicated file management operations can be reduced to one-line operations that can save hours of manual effort.
The find command will be one of your most useful Linux utilities, whether you are cleaning up disk space, auditing system security, dealing with development projects or automating system maintenance.