🔍 Command: find (Search for Files and Directories)
While grep searches for text inside files, the find command is used to search for the files themselves based on their attributes (name, size, age, permissions, etc.). It is one of the most versatile and powerful tools in the Linux filesystem.
1. The "Why"
As your projects grow, you will eventually forget where you saved a specific file.
- You might remember the name of a Java class but not which folder it's in.
- You might need to find all
.pdffiles larger than 100MB to clear space. - You might need to find every file you modified in the last 24 hours to track your progress on your e-book.
- You might want to find every directory with "777" permissions (which is a security risk) and fix them.
2. Basic Syntax
find [PATH] [EXPRESSION]
- Path: Where to start looking (e.g.,
.for current folder,/for the whole system). - Expression: What you are looking for (name, size, type, etc.).
3. Most Useful Flags
| Flag | Name | What it does |
|---|---|---|
-name |
Name | Search by filename (case-sensitive). |
-iname |
Insensitive Name | Search by filename (ignores uppercase/lowercase). |
-type |
Type | Filter by type: f for files, d for directories. |
-size |
Size | Search by file size (e.g., +10M for over 10MB). |
-mtime |
Modified Time | Search by how many days ago it was modified. |
-exec |
Execute | Run a command on every file that is found. |
4. Practical Examples
A. Finding a File by Name
To find a file named MainActivity.java starting from your current folder:
find . -name "MainActivity.java"
B. Finding All Files of a Certain Extension
To find every LaTeX file in your home directory:
find ~ -iname "*.tex"
C. Finding Large Files
If your Arch Linux partition is getting full, find files larger than 500MB:
find /home -type f -size +500M
D. Finding Recently Modified Files
To see which files you worked on in the last 2 days:
find . -mtime -2
E. The Power Move: Finding and Acting
You can find files and immediately do something to them. For example, to find all .tmp files and delete them:
find . -name "*.tmp" -exec rm {} \;
(The {} is a placeholder for the filename found, and \; ends the command).
5. Pro-Tips
- Permission Denied: If you search the root
/, you will see many "Permission denied" errors. Usesudoor redirect errors to nowhere:find / -name "secret.txt" 2>/dev/null. - Combine Criteria: You can look for "files that are over 10MB AND end in .mp4":
find . -type f -size +10M -name "*.mp4" - Arch Linux Context: You'll use this to find lost configuration files or to clean up the
~/.cachedirectory. It’s also helpful for finding where a specific package installed its files if you didn't usepacman -Ql.
6. Summary Reference
| Goal | Command |
|---|---|
| Find file by name | find . -name "filename" |
| Find only directories | find . -type d -name "name" |
| Find files modified today | find . -mtime -1 |
| Find empty files | find . -empty |
| Find files and delete them | find . -name "*.log" -delete |