~2 min read • Updated Jul 16, 2025
Linux organizes countless files within its directory structure. To search and manage them efficiently, tools like locate, find, xargs, touch, and stat are invaluable. This guide introduces their capabilities and practical use cases.
locate: Fast Name-Based Search
locate quickly searches file paths using a pre-built database:
locate bin/zipCombine with grep for filtering:
locate zip | grep binThe database may not reflect recent files. To refresh it manually:
sudo updatedbfind: Precision-Based File Search
find searches directories recursively based on attributes:
find ~ | wc -lBasic Filtering
find ~ -type d | wc -l
find ~ -type f | wc -lFile Type Definitions
| Type | Description |
|---|---|
| b | Block device |
| c | Character device |
| d | Directory |
| f | Regular file |
| l | Symbolic link |
Search for Large JPG Files
find ~ -type f -name "*.JPG" -size +1M | wc -lSize Units
| Unit | Description |
|---|---|
| b | 512-byte blocks |
| c | Bytes |
| w | 2-byte words |
| k | Kilobytes |
| M | Megabytes |
| G | Gigabytes |
Common find Tests
| Test | Description |
|---|---|
| -cmin | Files modified n minutes ago |
| -cnewer | Newer than reference file |
| -empty | Empty files or directories |
| -name | Matches filename pattern |
| -iname | Case-insensitive pattern |
| -user | Owned by specific user |
Combining Tests with Operators
find ~ \( -type f -not -perm 0600 \) -or \( -type d -not -perm 0700 \)Logical Operators
| Operator | Function |
|---|---|
| -and / -a | Both conditions must be true |
| -or / -o | Either condition is true |
| -not / ! | Negates a condition |
| () | Groups conditions (escaped with \) |
Built-In Actions
| Action | Description |
|---|---|
| -delete | Removes matching files |
| -ls | Displays details via ls -dils |
| Prints full file path | |
| -quit | Stops after first match |
Delete All .bak Files
find ~ -type f -name '*.bak' -deleteCustom Actions with -exec
find ~ -type f -name 'foo*' -exec ls -l '{}' ';'Interactive Execution with -ok
find ~ -type f -name 'foo*' -ok ls -l '{}' ';'Efficient Execution
find ~ -type f -name 'foo*' -exec ls -l '{}' +find ~ -type f -name 'foo*' -print | xargs ls -lHandling Special Characters
find ~ -iname '*.jpg' -print0 | xargs --null ls -lPractical Example
mkdir -p playground/dir-{001..100}
touch playground/dir-{001..100}/file-{A..Z}Search for Specific File
find playground -type f -name 'file-A' | wc -lUse touch and stat
touch playground/timestamp
stat playground/timestampUpdate Timestamp of Specific Files
find playground -type f -name 'file-B' -exec touch '{}' ';'find playground -type f -newer playground/timestampFix File and Directory Permissions
find playground \( -type f -not -perm 0600 -exec chmod 0600 '{}' ';' \) -or \( -type d -not -perm 0700 -exec chmod 0700 '{}' ';' \)Useful find Options
| Option | Description |
|---|---|
| -depth | Process files before their parent directories |
| -maxdepth | Limits how deep find descends |
| -mindepth | Skips initial levels before applying tests |
| -mount | Prevents crossing into other filesystems |
| -noleaf | Disables UNIX filesystem optimization |
Conclusion
loc
Written & researched by Dr. Shahin Siami