~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/zip

Combine with grep for filtering:


locate zip | grep bin

The database may not reflect recent files. To refresh it manually:


sudo updatedb

find: Precision-Based File Search


find searches directories recursively based on attributes:


find ~ | wc -l

Basic Filtering


find ~ -type d | wc -l
find ~ -type f | wc -l

File Type Definitions


TypeDescription
bBlock device
cCharacter device
dDirectory
fRegular file
lSymbolic link

Search for Large JPG Files


find ~ -type f -name "*.JPG" -size +1M | wc -l

Size Units


UnitDescription
b512-byte blocks
cBytes
w2-byte words
kKilobytes
MMegabytes
GGigabytes

Common find Tests


TestDescription
-cminFiles modified n minutes ago
-cnewerNewer than reference file
-emptyEmpty files or directories
-nameMatches filename pattern
-inameCase-insensitive pattern
-userOwned by specific user

Combining Tests with Operators


find ~ \( -type f -not -perm 0600 \) -or \( -type d -not -perm 0700 \)

Logical Operators


OperatorFunction
-and / -aBoth conditions must be true
-or / -oEither condition is true
-not / !Negates a condition
()Groups conditions (escaped with \)

Built-In Actions


ActionDescription
-deleteRemoves matching files
-lsDisplays details via ls -dils
-printPrints full file path
-quitStops after first match

Delete All .bak Files


find ~ -type f -name '*.bak' -delete

Custom 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 -l

Handling Special Characters


find ~ -iname '*.jpg' -print0 | xargs --null ls -l

Practical 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 -l

Use touch and stat


touch playground/timestamp
stat playground/timestamp

Update Timestamp of Specific Files


find playground -type f -name 'file-B' -exec touch '{}' ';'

find playground -type f -newer playground/timestamp

Fix 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


OptionDescription
-depthProcess files before their parent directories
-maxdepthLimits how deep find descends
-mindepthSkips initial levels before applying tests
-mountPrevents crossing into other filesystems
-noleafDisables UNIX filesystem optimization

Conclusion


loc

Written & researched by Dr. Shahin Siami