Finding Files
"I know the file exists somewhere. But where?"
This is one of the most common problems in Linux. Let's solve it.
find - Search by Criteria
find searches directories recursively:
Syntax: find [where] [criteria] [action]
Basic Usage
Case-Insensitive
-iname ignores case.
By Type
Type options:
f= regular filed= directoryl= symbolic link
By Size
Size suffixes: c (bytes), k (KB), M (MB), G (GB)
By Time
Time Units
-mtime uses days. For minutes, use -mmin:
find . -mmin -60 # Modified in last hour
Combining Criteria
find with Actions
Delete Found Files
Dangerous
-delete is permanent. Always run without -delete first to see what would be deleted:
find . -name '*.tmp' # Preview
find . -name '*.tmp' -delete # Execute
Execute Command
{} is replaced with each found file. \; ends the command.
Modern Alternative: fd
fd is a faster, more user-friendly alternative to find:
# Install it
sudo apt install fd-find # Then use: fdfind (or fd on other distros)
fd is simpler: patterns don't need quoting, it ignores .git and hidden files by default, and has colorized output.
When to Use Which
fdfor interactive searching - faster, simpler syntaxfindfor scripts - available everywhere, more powerful optionslocatefor system-wide searches - fastest, but might be stale
locate - Faster, But Less Current
locate uses a pre-built database - much faster than find:
Database Can Be Stale
locate uses a database that updates daily (usually). New files won't appear until the database updates. Run sudo updatedb to refresh it manually.
Comparison
| Feature | find | locate |
|---|---|---|
| Speed | Slow (searches live) | Fast (uses database) |
| Freshness | Always current | Can be stale |
| Criteria | Many options | Name only |
| Actions | Can execute commands | Search only |
which - Find Executables
Find where a command lives:
whereis - Find Related Files
Find binary, source, and man page:
Which command finds files modified in the last 24 hours?
Key Takeaways
findsearches live - always current but slowerlocateuses a database - faster but may be stalefind . -name 'pattern'is the basic search- Add
-type ffor files only,-type dfor directories - Use
-size,-mtimeto filter by size and age - Preview before using
-deleteor-exec
Congratulations! You've completed Chapter 3: Working with Files.
Next chapter: Text Manipulation - grep, cut, sort, and the powerful tools that process text.