DevOps Blog

Mastering the Linux find Command: A Beginner's Guide

Basic Syntax of find

The general structure of the find command is:

find [path] [options] [expression]
  • path: The directory where the search starts (defaults to current directory . if omitted).
  • options: Modify search behavior (e.g., depth, follow symlinks).
  • expression: Defines what to search for (name, size, type, etc.).

1. Finding Files by Name

The most common use of find is locating files by their name.

Search for a file named example.txt in the current directory:

find . -name "example.txt"
  • -name is case-sensitive. Use -iname for case-insensitive search.

Find all .jpg files in /home/user:

find /home/user -name "*.jpg"

2. Finding Files by Type

You can filter results by file type using -type:

  • f → Regular file
  • d → Directory
  • l → Symbolic link

Find all directories inside /var:

find /var -type d

Find all symbolic links in /usr/bin:

find /usr/bin -type l

3. Finding Files by Size

Search for files based on their size with -size:

  • +100M → Larger than 100MB
  • -10k → Smaller than 10KB
  • 1024c → Exactly 1024 bytes

Find files larger than 50MB in /home:

find /home -size +50M

Find empty files in the current directory:

find . -type f -size 0

4. Finding Files by Modification Time

You can locate files modified within a certain timeframe:

  • -mtime n → Modified exactly n days ago
  • -mtime +n → Modified more than n days ago
  • -mtime -n → Modified less than n days ago

Find files modified in the last 7 days:

find /var/log -mtime -7

Find files modified more than 30 days ago:

find /backups -mtime +30

5. Combining Conditions with Logical Operators

You can combine multiple conditions using:

  • -and (default, can be omitted)
  • -or
  • -not

Find .conf files modified in the last 3 days:

find /etc -name "*.conf" -mtime -3

Find files that are not .txt files:

find . -not -name "*.txt"

6. Executing Commands on Found Files

You can perform actions on found files using -exec:

Delete all .tmp files older than 30 days:

find /tmp -name "*.tmp" -mtime +30 -exec rm {} \;
  • {} is a placeholder for the found file.
  • \; terminates the command.

Change permissions of all .sh files to executable:

find ~/scripts -name "*.sh" -exec chmod +x {} \;

7. Advanced: Using find with xargs

For better performance with large searches, pipe results to xargs:

Find and compress all .log files:

find /var/log -name "*.log" | xargs gzip

Conclusion

The find command is an essential tool for Linux users, offering powerful file-searching capabilities. By mastering its options, you can quickly locate and manage files efficiently.

Quick Cheat Sheet

| Command | Description |
|---------|-------------|
| find . -name "file" | Find by name |
| find /dir -type d | Find directories |
| find ~ -size +100M | Find large files |
| find /etc -mtime -7 | Find recent files |
| find . -exec rm {} \; | Delete found files |

Now that you understand the basics, experiment with find to streamline your Linux workflow!

What’s your favorite find trick? Let us know in the comments!