DEV Community

AttractivePenguin
AttractivePenguin

Posted on

The 15 Linux Find Commands That Will Save You Hours

The 15 Linux Find Commands That Will Save You Hours

If you have ever spent 20 minutes clicking through directories trying to locate a file, only to realize it was hidden three folders deep with a typo in the name—you are not alone. The find command is one of Linux's most powerful tools, yet most developers barely scratch its surface.

Here is the truth: mastering find isn't about memorizing obscure flags. It's about knowing the right combination for the job at hand. These 15 commands will handle 95% of your file-searching needs, from hunting down massive log files to bulk-renaming thousands of images.


The Core Syntax

Before we dive in, here is the anatomy of a find command:

find [path] [expression]
Enter fullscreen mode Exit fullscreen mode
  • path: Where to start searching (defaults to current directory .)
  • expression: What to match and what to do with matches

The magic happens in the expression. Let us explore.


1. Find Files by Name

The simplest and most common use case:

find . -name "*.log"
Enter fullscreen mode Exit fullscreen mode

This searches the current directory and all subdirectories for files ending in .log. The -name flag is case-sensitive.


2. Case-Insensitive Search

When you cannot remember if that config file was .YML or .yml:

find /etc -iname "*.yml"
Enter fullscreen mode Exit fullscreen mode

The -iname flag matches regardless of case—perfect for when file naming conventions are inconsistent.


3. Find by Type: Files vs. Directories

Sometimes you only want files, or only directories:

# Find only files
find . -type f -name "config*"

# Find only directories
find . -type d -name "node_modules"
Enter fullscreen mode Exit fullscreen mode

Use -type f for files and -type d for directories. This is especially useful when you want to clean up node_modules folders scattered across projects.


4. Find Files by Size

Hunting down disk space hogs? This one is a lifesaver:

# Files larger than 100MB
find . -size +100M

# Files smaller than 1KB (often temp files)
find . -size -1k

# Files exactly 50 bytes
find . -size 50c
Enter fullscreen mode Exit fullscreen mode

The + means "greater than," - means "less than," and no prefix means "exactly." Size units include k (KB), M (MB), G (GB), and c (bytes).


5. Find by Modification Time

Need to find what changed recently? Use -mtime:

# Files modified in the last 24 hours
find . -mtime -1

# Files modified more than 7 days ago
find . -mtime +7

# Files modified exactly 3 days ago
find . -mtime 3
Enter fullscreen mode Exit fullscreen mode

This is perfect for cleaning up old logs or finding files you touched last week but forgot where you saved them.


6. Find by Access Time

When was a file last read? Use -atime:

# Files not accessed in 30 days
find . -atime +30
Enter fullscreen mode Exit fullscreen mode

Great for identifying stale files that might be safe to archive or delete.


7. Find by Permissions

Find files with specific permission settings:

# Find world-writable files (security risk!)
find /var/www -perm -o+w

# Find executable files
find . -type f -perm /u+x
Enter fullscreen mode Exit fullscreen mode

The -perm flag is essential for security audits.


8. Find Empty Files and Directories

Those pesky zero-byte files and empty folders:

# Find empty files
find . -type f -empty

# Find empty directories
find . -type d -empty
Enter fullscreen mode Exit fullscreen mode

Combine with -delete to clean them up instantly.


9. Find by Owner

Track down files owned by a specific user:

# Find files owned by root
find /home -user root

# Find files owned by a group
find . -group docker
Enter fullscreen mode Exit fullscreen mode

Useful for permission troubleshooting and security audits.


10. Find and Execute Commands

This is where find becomes a power tool. Use -exec to run commands on matches:

# Delete all .log files
find . -name "*.log" -exec rm {} \;

# Compress all PDF files individually
find . -name "*.pdf" -exec gzip {} \;

# Change permissions on all scripts
find . -name "*.sh" -exec chmod +x {} \;
Enter fullscreen mode Exit fullscreen mode

The {} is a placeholder for each matched file. \; ends the command.


11. Find and Execute Efficiently

For better performance with large result sets, use -exec with +:

# Pass all matches to one command (more efficient)
find . -name "*.jpg" -exec chmod 644 {} +
Enter fullscreen mode Exit fullscreen mode

This batches files into fewer command invocations, significantly faster for thousands of files.


12. Find and Delete Safely

A shortcut for the common "find and remove" pattern:

# Delete all .tmp files (be careful!)
find . -name "*.tmp" -delete
Enter fullscreen mode Exit fullscreen mode

This is safer than -exec rm {} \; because it is atomic and built into find.


13. Combine Conditions with OR Logic

Need to find multiple file types at once? Use -o (or):

# Find all images (jpg, png, gif)
find . \( -name "*.jpg" -o -name "*.png" -o -name "*.gif" \)
Enter fullscreen mode Exit fullscreen mode

Important: Parentheses must be escaped with backslashes, and the entire group should be quoted or escaped.


14. Combine Conditions with AND Logic

By default, conditions are ANDed together. But you can be explicit:

# Find large log files (both conditions must match)
find . -name "*.log" -a -size +10M
Enter fullscreen mode Exit fullscreen mode

This finds log files that are also larger than 10MB.


15. Limit Search Depth

Do not want to search recursively forever? Use -maxdepth:

# Search only the current directory (no subdirectories)
find . -maxdepth 1 -name "*.txt"

# Search two levels deep
find . -maxdepth 2 -type d -name "node_modules"
Enter fullscreen mode Exit fullscreen mode

This dramatically speeds up searches on large directory trees.


Bonus: Find with Grep

Combine find with grep to search file contents:

# Find all .py files containing "TODO"
find . -name "*.py" -exec grep -l "TODO" {} +

# Find files with a specific string in their content
find . -type f -exec grep -l "api_key" {} +
Enter fullscreen mode Exit fullscreen mode

The -l flag makes grep output only filenames, not matching lines.


Real-World Scenarios

Scenario 1: Clean Up Old Log Files

Your server is running out of disk space due to accumulated logs:

# Find and delete log files older than 30 days
find /var/log -name "*.log" -mtime +30 -delete

# Or archive them first
find /var/log -name "*.log" -mtime +30 -exec tar -rf old_logs.tar {} \;
Enter fullscreen mode Exit fullscreen mode

Scenario 2: Find Files Eating Your Disk Space

When df -h shows your disk is full but you do not know why:

# Find the 10 largest files in your home directory
find ~ -type f -exec du -h {} + | sort -rh | head -10

# Or simpler: find files larger than 500MB
find ~ -type f -size +500M -exec ls -lh {} \;
Enter fullscreen mode Exit fullscreen mode

Scenario 3: Find Recently Modified Files

Something broke and you need to know what changed:

# Files modified in the last hour
find /var/www -type f -mmin -60

# Files modified in the last 24 hours
find /var/www -type f -mtime -1
Enter fullscreen mode Exit fullscreen mode

This is invaluable for debugging—especially when a deployment goes wrong.

Scenario 4: Batch File Operations

Need to move all images to a single folder?

# Find all images and move to ~/Pictures
find ~ -type f \( -name "*.jpg" -o -name "*.png" \) -exec mv {} ~/Pictures/ \;
Enter fullscreen mode Exit fullscreen mode

Scenario 5: Find and Fix Permissions

After copying files from Windows, everything is executable:

# Remove execute permission from all files (not directories)
find . -type f -exec chmod -x {} +

# Set proper permissions for web directories
find /var/www -type d -exec chmod 755 {} \;
find /var/www -type f -exec chmod 644 {} \;
Enter fullscreen mode Exit fullscreen mode

FAQ

Q: What is the difference between find and locate?

find searches the filesystem in real-time, while locate uses a pre-built database. locate is faster but may show outdated results. Use find for accuracy, locate for speed.

Q: How do I exclude a directory from the search?

Use -prune:

# Search everything except node_modules
find . -name "node_modules" -prune -o -name "*.js" -print
Enter fullscreen mode Exit fullscreen mode

Q: Can I use regular expressions?

Yes! Use -regex:

find . -regex ".*\.[0-9]+\.log"
Enter fullscreen mode Exit fullscreen mode

This matches files like error.20240101.log.

Q: Why does -exec need \;?

The \; marks the end of the command. Without it, find does not know where your command ends and its own options begin.

Q: How do I see the command before running it?

Add -ok instead of -exec:

# Prompt before each deletion
find . -name "*.tmp" -ok rm {} \;
Enter fullscreen mode Exit fullscreen mode

This asks for confirmation before executing.


Quick Reference Cheat Sheet

# By name (case-sensitive)
find . -name "*.log"

# By name (case-insensitive)
find . -iname "*.LOG"

# By type
find . -type f          # files only
find . -type d          # directories only

# By size
find . -size +100M      # larger than 100MB
find . -size -1k        # smaller than 1KB

# By time
find . -mtime -7        # modified within 7 days
find . -mtime +30       # modified 30+ days ago
find . -mmin -60        # modified within 60 minutes

# By permissions
find . -perm 755        # exact permissions
find . -perm -o+w       # world-writable

# Execute commands
find . -name "*.log" -exec rm {} \;
find . -name "*.log" -exec rm {} +    # more efficient

# Combine conditions
find . \( -name "*.jpg" -o -name "*.png" \)   # OR
find . -name "*.log" -a -size +10M            # AND

# Limit depth
find . -maxdepth 2 -name "*.txt"
Enter fullscreen mode Exit fullscreen mode

Conclusion

The find command is one of those tools that pays dividends every single day. Once you internalize these patterns, you will stop manually browsing directories and start letting the command line do the heavy lifting.

Start with the basics—searching by name and type. Then add size and time filters. Finally, master -exec to transform find from a search tool into an automation powerhouse.

The next time you need to clean up old logs, find disk space hogs, or locate that config file you swear you saved somewhere, remember: find has your back.

What is your favorite find command? Drop it in the comments—I am always adding to my collection.


Happy file hunting! 🔍

Top comments (0)