locate and find Commands
Think of searching for files on your Linux system like trying to find items in your home. The locate command is like asking someone who keeps a catalog of everything in your house - it's incredibly fast but might not know about things you've recently bought. The find command is like searching room by room yourself - it takes longer but will find even the newest items and can search based on detailed criteria like size, color, or when you got them. These powerful search tools help you track down files no matter how deeply they're tucked away in your system.
Quick Reference
| Command | What It Does | Common Use |
|---|---|---|
locate pattern |
Searches for files using a prebuilt database | Quickly finding files by name when speed matters |
updatedb |
Updates the locate database | Making sure locate has current information |
find path criteria |
Searches for files in real-time based on various criteria | Finding files based on detailed conditions or recent changes |
When to Use These Commands
- When you need to find a file but don't know where it's located
- When you need to find files based on specific characteristics (size, date, permissions)
- When you need to perform actions on multiple files that match certain criteria
- When troubleshooting missing or hidden files
- When cleaning up a system by finding old or large files
The locate Command
Think of locate as the lightning-fast card catalog in a library. It doesn't search your actual files - instead, it checks a pre-built index that gets updated regularly. This makes locate extremely fast but means it might miss recent changes if the database hasn't been updated. Another limitation is that it can only search by filename, not by other attributes like file size, permissions, or modification time. For those searches, find is needed.
| Option | What It Does | When to Use |
|---|---|---|
-i |
Makes the search case-insensitive | When you can't remember the exact capitalization |
-r |
Uses regular expressions for searching | When you need complex pattern matching |
-n NUMBER |
Limits results to a specific number | When you only need a few results from a large set |
-e |
Shows only files that actually exist | When you want to filter out deleted files still in the database |
-c |
Shows only the count of matching files | When you just need to know how many matches exist |
Practical Examples
# Find all files containing "homework" in their name
locate homework
# Lists all files with "homework" in their path
# Find files containing "report" regardless of case
locate -i report
# Finds "Report.pdf", "REPORT.txt", "weekly_report", etc.
# Find at most 5 PDF files
locate -n 5 "*.pdf"
# Shows only the first 5 PDF files found
# Find all configuration files using regular expressions
locate -r "/etc/.*\.conf$"
# Finds all .conf files in /etc and its subdirectories
# Count how many Python files you have
locate -c "*.py"
# Shows just the number of Python files found
# Update the locate database (do this after creating new files)
sudo updatedb
# Refreshes the database so locate will find new files
The find Command
Think of find as a detective that searches your entire system in real-time. Unlike locate, it doesn't use a database but instead looks through your actual directories. This makes it more versatile and accurate for recent changes, but slower than locate, especially on large systems.
Basic Structure
The basic structure of a find command is:
find [starting directory] [options] [tests] [actions]
Common Tests (Search Criteria)
| Test | What It Does | When to Use |
|---|---|---|
-name "pattern" |
Matches files with the exact name/pattern | When you know the exact filename or pattern (case-sensitive) |
-iname "pattern" |
Like -name but case-insensitive | When you're unsure about capitalization |
-type f |
Matches only regular files | When you don't want directories, links, etc. |
-type d |
Matches only directories | When you're looking for folders only |
-size +10M |
Matches files larger than 10 megabytes | When looking for large files taking up space |
-size -10M |
Matches files smaller than 10 megabytes | When looking for small files |
-mtime -7 |
Files modified in the last 7 days | When looking for recently changed files |
-user username |
Files owned by a specific user | When looking for files belonging to someone |
-perm mode |
Files with specific permissions | When checking for security issues or access rights |
-empty |
Empty files or directories | When cleaning up wasted space |
Operators for Combining Tests
| Operator | What It Does | When to Use |
|---|---|---|
-and or just a space |
Both conditions must be true (logical AND) | When files must satisfy multiple criteria |
-or |
Either condition can be true (logical OR) | When looking for files that match any of several criteria |
! or -not |
Negates a condition (logical NOT) | When excluding certain files from results |
( ) |
Groups conditions (use with \( and \)) | When creating complex conditions with proper precedence |
Common Actions
| Action | What It Does | When to Use |
|---|---|---|
-print |
Prints matching files (default action) | When you just want to see what matches |
-delete |
Deletes matching files | When cleaning up files (use with caution!) |
-exec command {} \; |
Runs a command on each matching file | When you need to process matching files |
-exec command {} + |
Runs command once with all matches | More efficient than \; for many files |
xargs |
Runs a command on each file that matches your search criteria | When you need to process a large number of files |
Size Options
| Size Format | What It Means | Example |
|---|---|---|
+n |
Greater than n units | -size +10M (larger than 10MB) |
-n |
Less than n units | -size -1M (smaller than 1MB) |
n |
Exactly n units | -size 100k (exactly 100KB) |
c (bytes) |
Size in bytes | -size 1024c (1024 bytes) |
k (kilobytes) |
Size in kilobytes | -size 10k (10KB) |
M (megabytes) |
Size in megabytes | -size 5M (5MB) |
G (gigabytes) |
Size in gigabytes | -size 1G (1GB) |
Practical Examples
In the practical examples below we assume the find is look in only in the directory it is running in. So starting directory is the current directory, which is denoted by a period (.).
# Find all text files in the current directory
find . -name "*.txt"
# Lists all .txt files in the current directory and subdirectories
# Find large files that might be wasting space
find . -type f -size +100M
# Finds files larger than 100MB in the current directory
# Find files between 10MB and 100MB in size (using "and" logic)
find . -type f -size +10M -size -100M
# Finds files larger than 10MB but smaller than 100MB
# Find files modified in the last week but not in the last day
find . -type f -mtime -7 -not -mtime -1
# Finds files modified between 1 and 7 days ago
# Find recently modified files
find . -type f -mtime -2
# Finds files in the current directory modified in the last 2 days
# Find files with specific permissions
find . -type f -perm 0777
# Finds files with potentially insecure 777 permissions
# Find all empty files and directories
find . -empty
# Lists all empty files and directories in the current directory
# Find files owned by a specific user
find . -user john
# Finds files owned by user 'john'
# Find with complex conditions (find text files NOT owned by root)
find . -name "*.txt" -not -user root
# Finds text files that don't belong to root
# Find either PNG or JPG files (using "or" logic)
find . \( -name "*.png" -or -name "*.jpg" \)
# Finds all PNG or JPG files in the current directory
# Find executable files that are not shell scripts
find . -type f -executable -not -name "*.sh"
# Finds executable files that don't have the .sh extension
# Find files that match multiple criteria (AND example)
find . -type f -user john -and -mtime -30
# Finds files owned by john that were modified in the last 30 days
# Find docx files OR pdf files (OR example)
find . \( -name "*.docx" -o -name "*.pdf" \)
# Finds all Word documents or PDF files
# Find files with complex conditions using AND, OR, and NOT together
find . -type f \( -name "*.java" -o -name "*.py" \) -not -path "*/test/*"
# Finds Java or Python files that are not in test directories
Using find with -exec and xargs
One of the most powerful features of find is the ability to perform actions on the files you find. You can do this with the -exec option or by piping to xargs.
Using -exec
-exec is used to run a command on each file that matches your search criteria. The {} placeholder is replaced with each filename, and \; marks the end of the command.
For Example:
in the code find my_project -type f -exec echo "Processing file: {} " \; the -exec option will run the echo "Processing file: {} " command on each file that matches the search criteria. The {} placeholder is replaced with each filename, and \; marks the end of the command. So for example if there are 3 files in the my_project directory that match the search criteria, the command will be run 3 times, once for each file.
# Find and delete all .tmp files
find /tmp -name "*.tmp" -exec rm {} \;
# The {} is replaced with each filename, and \; marks the end of the command
# Make all shell scripts executable
find ~/scripts -name "*.sh" -exec chmod +x {} \;
# Adds executable permission to all .sh files
# Find and copy all configuration files to a backup directory
find /etc -name "*.conf" -exec cp {} ~/backup/ \;
# Copies each .conf file to ~/backup/
# Find and list details about PDF files
find ~/Documents -name "*.pdf" -exec ls -lh {} \;
# Shows detailed listing of each PDF file
Using xargs
xargs is used to run a command on each file that matches your search criteria. It is often used in combination with find to perform actions on the files you find.
For Example:
in the code find . -name "*.txt" | xargs wc -l the find command will locate all text files in the current directory, and the xargs command will run the wc -l command on each file. The | operator is used to pipe the output of the find command to the xargs command. So for example if there are 3 text files in the current directory, the command will be run 3 times, once for each file.
# Find and delete all .bak files (alternative to -exec)
find /home -name "*.bak" | xargs rm
# Pipes filenames to xargs which runs rm with those filenames as arguments
# Count lines of code in all Python files
find . -name "*.py" | xargs wc -l
# Counts lines in all Python files
# Create a tar archive of all HTML files
find . -name "*.html" | xargs tar -czf html_files.tar.gz
# Archives all HTML files into a single compressed file
# Replace text in multiple files
find . -name "*.txt" | xargs sed -i 's/old text/new text/g'
# Replaces "old text" with "new text" in all text files
Tips for Success
- Run
sudo updatedbregularly to keep thelocatedatabase current - Use
locatefor quick searches andfindwhen you need more detailed criteria - Test complex
findcommands without destructive actions first (remove-deleteor-exec) - When using
-execwith potentially destructive commands, run in a test directory first - Use quotes around patterns with wildcards to prevent shell expansion
- Add the
-type foption to find only files (not directories) when appropriate - Use
find -maxdepthto limit how deep find searches to improve performance - Remember that
locateshows file paths, not just filenames
Common Mistakes to Avoid
- Forgetting to update the locate database after creating new files (
sudo updatedb) - Not escaping parentheses in
findcommands: use\( \)instead of( ) - Forgetting to terminate
-execcommands with\; - Using
findwithout specifying a starting directory (defaults to current directory) - Not using quotes around patterns, causing the shell to expand wildcards before
findruns - Using
-deletewithout first verifying which files will be deleted - Assuming
locatecan search for files by criteria other than name - Running complex find commands as root without testing them first
Best Practices
- Use
locatefor simple filename searches andfindfor everything else - Combine multiple tests to narrow down search results precisely
- Use
-exec ... {} +instead of-exec ... {} \;when possible for better performance - Create aliases for commonly used find commands in your .bashrc
- When searching large filesystems, start with more specific directories to improve speed
- Use
2>/dev/nullto suppress permission denied errors when searching system directories - For complex file operations, consider using
findto create a list, review it, then execute - Document complex find commands you create for future reference
Real-World Scenarios
Cleaning Up Temporary Files
# Find and remove files older than 30 days in /tmp
find /tmp -type f -mtime +30 -delete
# Explanation:
# This finds all regular files (-type f) in the /tmp directory
# that haven't been modified in more than 30 days (-mtime +30)
# and deletes them (-delete)
# This is a common system maintenance task to free up disk space
Finding and Fixing Permissions
# Find and fix world-writable files
find . -type f -perm -0002 -exec chmod o-w {} \;
# Explanation:
# This finds all regular files in the current directory
# with world-writable permissions (potentially insecure)
# and removes the world-writable bit
# This improves security by preventing unauthorized users from modifying files
Creating a Backup of Important Files
# Back up all configuration files modified today
find . -name "*.conf" -type f -mtime 0 -exec cp {} ~/config_backup/ \;
# Explanation:
# This finds all .conf files in the current directory
# that were modified today (-mtime 0)
# and copies them to a backup directory
# This helps preserve important changes before making system changes