Linux CLI 35 ๐ง locate and find commands
locate aaa.txt
locate *.txt -n 5
locate *.TXT -n 5 -i
locate *.TXT -c
find . -name aaa.txt
find . -type d -name dir1
find . -type d -name 'd*'
find . -user root
locate and find both search for files, but they work in completely different ways. locate queries a pre-built database โ it’s blazing fast but only as current as the last database update. find walks the filesystem in real time โ it’s slower but always accurate and far more powerful.
Key point: Use locate when you want a quick answer and don’t mind a slightly stale index. Use find when you need exact results, complex criteria, or the ability to act on the results.
a – locate command
locate is used to search for files and directories. It is similar to find but only searches on the filename โ it can’t filter by size, owner, or permissions. Instead, it reads from a database (/var/lib/mlocate/mlocate.db or similar) that’s built by updatedb.
Because it reads a database instead of the live filesystem, locate is extremely fast โ it can search millions of files in milliseconds. The trade-off is that the database is only as fresh as the last updatedb run.
| Command | Description |
|---|---|
sudo updatedb | Updates locate’s database |
locate aaa.txt | Searches for aaa.txt |
locate *.txt -n 5 | Searches for .txt files, shows only 5 |
locate *.TXT -n 5 -i | Case-insensitive match |
locate *.TXT -c | Returns only the number of matches |
Common options:
| Option | Purpose |
|---|---|
-n N | Limit results to N |
-i | Case-insensitive |
-c | Count matches only |
-l N | Limit the number (same as -n) |
-q | Quiet mode |
-r | Use a regex pattern |
Examples:
# Update the database first (needs root)
$ sudo updatedb
# Find a file by name
$ locate aaa.txt
/home/kronos/documents/aaa.txt
/home/kronos/backup/aaa.txt
# Limit to 5 results
$ locate *.txt -n 5
/home/kronos/notes.txt
/home/kronos/todo.txt
/home/kronos/readme.txt
/home/kronos/config.txt
/home/kronos/backup.txt
# Case-insensitive search
$ locate *.TXT -n 5 -i
/home/kronos/notes.txt
/home/kronos/README.TXT
/home/kronos/Todo.TXT
/home/kronos/config.txt
/home/kronos/backup.txt
# Count matches only
$ locate *.TXT -c
1234
# Regex pattern
$ locate -r '\.txt$'
/home/kronos/notes.txt
/home/kronos/todo.txt
...
# Quiet mode โ no error for missing DB
$ locate -q aaa.txt
# Locate a binary
$ locate -b '\ls'
/usr/bin/ls
/bin/ls
Note: On some systems,
locate‘s database is updated automatically by a cron job or systemd timer. On others, you have to runsudo updatedbmanually. If a file you just created doesn’t show up, that’s why.
Privacy tip: The locate database is world-readable by default, which means any user can see every filename on the system. On multi-user systems, you may want to restrict it.
b – find command
find is used to search for files and directories. The search is based on various criteria like name, size, date, owner, permissions, and more. Unlike locate, find walks the live filesystem, so results are always current.
Syntax:
find [path] [options] [expression]
| Command | Description |
|---|---|
find . -name aaa.txt | Searches for aaa.txt in current directory |
find . -type d -name dir1 | Searches for dir1 among directories |
find . -type d -name 'd*' | Searches for directories starting with d |
find . -user root | Searches for files/directories owned by root |
Common options:
| Option | Purpose |
|---|---|
-name PATTERN | Match by filename (case-sensitive) |
-iname PATTERN | Match by filename (case-insensitive) |
-type f / d / l | File, directory, or symlink |
-user USER | Owned by a specific user |
-group GROUP | Owned by a specific group |
-perm MODE | Match specific permissions |
-mtime N | Modified N days ago |
-atime N | Accessed N days ago |
-size N | Match specific size |
-maxdepth N | Limit directory depth |
-exec CMD {} \; | Run a command on each result |
Examples:
# Find a file by name in the current directory tree
$ find . -name aaa.txt
./documents/aaa.txt
./backup/aaa.txt
# Case-insensitive name match
$ find . -iname aaa.txt
./documents/aaa.txt
./Documents/AAA.TXT
# Find only directories named dir1
$ find . -type d -name dir1
./projects/dir1
./archive/dir1
# Find directories starting with "d"
$ find . -type d -name 'd*'
./documents
./downloads
./data
./projects/dir1
# Find files owned by root
$ find . -user root
./system/config
./root/.bashrc
# Find files modified in the last 7 days
$ find . -mtime -7
./notes.txt
./recent.log
# Find files larger than 100 MB
$ find . -size +100M
./videos/movie.mp4
./backups/archive.tar.gz
# Find files with specific permissions (e.g., 644)
$ find . -perm 644
./readme.txt
./config.ini
# Find empty files
$ find . -type f -empty
./empty.txt
./placeholder
# Find and delete old log files
$ find /var/log -name '*.log' -mtime +30 -delete
# Find and run a command on each result
$ find . -name '*.tmp' -exec rm {} \;
# Find files by size range (between 1 MB and 10 MB)
$ find . -size +1M -size -10M
./documents/report.pdf
./images/photo.jpg
# Limit search depth
$ find . -maxdepth 2 -name '*.conf'
./config/app.conf
./config/db.conf
# Combine criteria with AND (default) and OR
$ find . -type f -name '*.log' -o -name '*.txt'
./app.log
./notes.txt
# Find files newer than a specific file
$ find . -newer reference.txt
./recent-change.txt
Warning:
findwith-exec rmor-deleteis destructive. Always run the search first without the delete action to see what would be removed.
Complete Example Session
# ============================================
# PART 1: LOCATE โ FAST DATABASE SEARCH
# ============================================
# Update the database
$ sudo updatedb
# Find a file by name
$ locate aaa.txt
/home/kronos/documents/aaa.txt
/home/kronos/backup/aaa.txt
# Limit results
$ locate *.txt -n 5
/home/kronos/notes.txt
/home/kronos/todo.txt
/home/kronos/readme.txt
/home/kronos/config.txt
/home/kronos/backup.txt
# Case-insensitive
$ locate *.TXT -n 5 -i
/home/kronos/notes.txt
/home/kronos/README.TXT
/home/kronos/Todo.TXT
/home/kronos/config.txt
/home/kronos/backup.txt
# Count matches
$ locate *.TXT -c
1234
# Regex
$ locate -r '\.log$'
/var/log/syslog
/var/log/auth.log
/var/log/kern.log
# ============================================
# PART 2: FIND โ REAL-TIME FILESYSTEM SEARCH
# ============================================
# Basic name search
$ find . -name aaa.txt
./documents/aaa.txt
./backup/aaa.txt
# Case-insensitive
$ find . -iname aaa.txt
./documents/aaa.txt
./Documents/AAA.TXT
# Only directories
$ find . -type d -name dir1
./projects/dir1
./archive/dir1
# Directories starting with "d"
$ find . -type d -name 'd*'
./documents
./downloads
./data
./projects/dir1
# Files owned by root
$ find . -user root
./system/config
./root/.bashrc
# Files modified in the last 7 days
$ find . -mtime -7
./notes.txt
./recent.log
# Files larger than 100 MB
$ find . -size +100M
./videos/movie.mp4
./backups/archive.tar.gz
# Empty files
$ find . -type f -empty
./empty.txt
./placeholder
# ============================================
# PART 3: FIND + ACTION
# ============================================
# Dry run first
$ find /var/log -name '*.log' -mtime +30
/var/log/old.log
/var/log/archive-2023.log
# Then delete
$ sudo find /var/log -name '*.log' -mtime +30 -delete
# Run a command on each result
$ find . -name '*.tmp' -exec rm {} \;
# Run a command with a confirmation prompt
$ find . -name '*.tmp' -ok rm {} \;
< rm ... ./temp1.tmp > ? y
< rm ... ./temp2.tmp > ? y
# Print size in human-readable form
$ find . -name '*.log' -exec ls -lh {} \;
-rw-r--r-- 1 kronos kronos 1.2M Jan 15 10:00 ./app.log
-rw-r--r-- 1 kronos kronos 4.5M Jan 15 09:00 ./sys.log
Quick Reference
locate
| Command | Purpose |
|---|---|
sudo updatedb | Update the database |
locate FILE | Find a file by name |
locate -n N FILE | Limit to N results |
locate -i FILE | Case-insensitive |
locate -c FILE | Count matches |
locate -r REGEX | Regex match |
locate -q FILE | Quiet mode |
find โ Name & Type
| Command | Purpose |
|---|---|
find . -name FILE | By name |
find . -iname FILE | Case-insensitive |
find . -type f | Files only |
find . -type d | Directories only |
find . -type l | Symlinks only |
find . -maxdepth N | Limit depth |
find โ Ownership & Permissions
| Command | Purpose |
|---|---|
find . -user USER | By owner |
find . -group GROUP | By group |
find . -perm MODE | By permissions |
find โ Time
| Command | Purpose |
|---|---|
find . -mtime -N | Modified in last N days |
find . -mtime +N | Modified more than N days ago |
find . -atime -N | Accessed in last N days |
find . -newer FILE | Newer than another file |
find โ Size
| Command | Purpose |
|---|---|
find . -size +100M | Larger than 100 MB |
find . -size -1M | Smaller than 1 MB |
find . -size 10k | Exactly 10 KB |
find . -empty | Empty files/dirs |
find โ Action
| Command | Purpose |
|---|---|
find . -exec CMD {} \; | Run command on each result |
find . -exec CMD {} + | Batch command (faster) |
find . -ok CMD {} \; | Ask before each |
find . -delete | Delete matches |
find . -print | Print (default) |
Best Practices
โ Do This:
# Run updatedb before locate for fresh results
sudo updatedb && locate file.txt # โ
# Use find for accurate, real-time results
find . -name file.txt # โ
# Quote wildcards in find so the shell doesn't expand them
find . -name '*.txt' # โ
# Use -exec ... {} + for speed
find . -name '*.log' -exec rm {} + # โ
# Dry run first with -delete / -exec rm
find /tmp -name '*.tmp' # โ
find /tmp -name '*.tmp' -delete # โ
# Use -maxdepth to avoid huge scans
find . -maxdepth 2 -name '*.conf' # โ
# Prefer -iname when case doesn't matter
find . -iname 'readme*' # โ
โ Don’t Do This:
# Don't rely on locate for files just created
locate newfile.txt # โ stale DB
# Don't forget to quote wildcards
find . -name *.txt # โ shell expands first
# Don't run find / as a regular user (huge + errors)
find / -name foo # โ permission noise
# Don't use find -delete without checking first
find . -name '*.bak' -delete # โ may remove wanted files
# Don't use -exec without {} or \;
find . -name '*.log' -exec rm # โ missing args
# Don't assume locate is installed
locate file.txt # โ may need mlocate
Common Pitfalls
| Pitfall | Problem | Solution |
|---|---|---|
locate finds old files | Stale DB | sudo updatedb |
locate finds deleted files | Stale DB | sudo updatedb |
find misses a file | Wrong path | Check starting dir |
| Wildcards expanded by shell | Wrong results | Quote them: '*.txt' |
find / slow + noisy | Scanning everything | Limit path and use 2>/dev/null |
-mtime -7 misread | Means “within 7 days” | +7 = older than 7 days |
-size +100M no match | Case-sensitive unit | Use M, k, G correctly |
-exec runs per file | Slow on many files | Use {} + instead of {} \; |
Real-World Examples
1. Find a File by Name
$ locate aaa.txt
/home/kronos/documents/aaa.txt
/home/kronos/backup/aaa.txt
2. Find Only Directories Named logs
$ find /var -type d -name logs
/var/logs
/var/lib/docker/containers/logs
3. Find Files Owned by Root
$ find /home -user root
/home/kronos/.ssh/authorized_keys
/home/kronos/.sudo_as_admin_successful
4. Find Files Modified in the Last Day
$ find . -mtime -1
./today.log
./recent-edit.txt
5. Find and Delete Old Logs
$ find /var/log -name '*.log' -mtime +30 -delete
6. Find Large Files
$ find / -type f -size +1G 2>/dev/null
/home/kronos/videos/movie.mp4
/var/lib/docker/overlay2/.../layer.tar
7. Find Empty Files and Directories
$ find . -empty
./empty.txt
./placeholder
./emptydir
8. Find Files Newer Than Another File
$ find . -newer /tmp/reference.txt
./changed-since.txt
./new-file.log
9. Run a Command on Each Match
$ find . -name '*.txt' -exec wc -l {} \;
123 ./notes.txt
45 ./todo.txt
10. Find and Copy Results
$ find . -name '*.log' -exec cp {} /backup/logs/ \;
11. Combine with grep and xargs
# Find files containing "TODO"
$ find . -name '*.py' -print0 | xargs -0 grep -l 'TODO'
./src/main.py
./src/utils.py
12. Find Files with Specific Permissions
# World-writable files (security check)
$ find / -type f -perm -o+w 2>/dev/null
/tmp/unsafe.txt
Visual: locate vs find
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ locate (database lookup) โ
โ โ
โ /var/lib/mlocate/mlocate.db โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ /home/kronos/notes.txt โ โ
โ โ /home/kronos/readme.txt โ โ
โ โ /var/log/syslog โ โ
โ โ ... โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ $ locate notes.txt โ instant โ
โ โ ๏ธ only as fresh as updatedb โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ find (live filesystem) โ
โ โ
โ $ find . -name notes.txt โ
โ walks every directory in real time โ
โ โ
โ โ
always current โ
โ โ
filter by size, time, owner, perms โ
โ โ
can act on results โ
โ โ ๏ธ slower on big trees โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Summary
| Command | Purpose | Example |
|---|---|---|
sudo updatedb | Update locate DB | sudo updatedb |
locate FILE | Find by name | locate aaa.txt |
locate -n N FILE | Limit results | locate *.txt -n 5 |
locate -i FILE | Case-insensitive | locate *.TXT -i |
locate -c FILE | Count matches | locate *.TXT -c |
find . -name FILE | Find by name | find . -name aaa.txt |
find . -iname FILE | Case-insensitive | find . -iname aaa.txt |
find . -type d | Directories only | find . -type d -name dir1 |
find . -type f | Files only | find . -type f -name '*.txt' |
find . -user USER | By owner | find . -user root |
find . -group GROUP | By group | find . -group sudo |
find . -perm MODE | By permissions | find . -perm 644 |
find . -mtime -N | Modified recently | find . -mtime -7 |
find . -atime -N | Accessed recently | find . -atime -1 |
find . -size +N | By size | find . -size +100M |
find . -empty | Empty files/dirs | find . -empty |
find . -maxdepth N | Limit depth | find . -maxdepth 2 |
find . -exec CMD {} \; | Run command | find . -name '*.tmp' -exec rm {} \; |
find . -delete | Delete matches | find . -name '*.bak' -delete |
Key takeaways:
locateis fast because it reads a pre-built database โ but the results can be stalesudo updatedbrefreshes the locate databaselocateonly matches on filename โ no size, owner, or time filtersfindwalks the live filesystem โ always accurate, far more powerful- Use
find . -name '*.txt'โ quote the wildcard so the shell doesn’t expand it first - Filter by type (
-type f/d/l), owner (-user), group (-group), permissions (-perm), time (-mtime/-atime), and size (-size) - Use
-exec CMD {} +for fast batch actions and{} \;for one-at-a-time -maxdepth Nkeeps find from scanning huge trees- Always dry run before combining
findwith-deleteor-exec rm - On modern systems,
findpiped toxargs -0or used with-print0handles filenames with spaces safely
Remember: locate is a search engine over an index โ instant but stale. find is a live walk โ slower but exact and infinitely flexible. Update the locate database after major changes. Quote wildcards in find. Test destructive commands before running them for real. Between the two, you can find any file on the system in seconds โ and do something useful with it once you’ve found it.
Stop using slow, ad-bloated tool sites! ๐คฎ
๐ Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
โ
Finance (Mortgage, Interest, Inflation)
โ
Tech (Base64, JSON, Dev Suite, IP)
โ
Health (BMI, BMR, TDEE)
โ
Productivity (Timer, Workspace, QR)
โก๏ธ Fast & Private
๐ No data leaves your device
๐ 100% Free
๐ Use it now: https://tools.kandz.me
๐ Bookmark itโyouโll need it later!