|

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.

CommandDescription
sudo updatedbUpdates locate’s database
locate aaa.txtSearches for aaa.txt
locate *.txt -n 5Searches for .txt files, shows only 5
locate *.TXT -n 5 -iCase-insensitive match
locate *.TXT -cReturns only the number of matches

Common options:

OptionPurpose
-n NLimit results to N
-iCase-insensitive
-cCount matches only
-l NLimit the number (same as -n)
-qQuiet mode
-rUse 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 run sudo updatedb manually. 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]
CommandDescription
find . -name aaa.txtSearches for aaa.txt in current directory
find . -type d -name dir1Searches for dir1 among directories
find . -type d -name 'd*'Searches for directories starting with d
find . -user rootSearches for files/directories owned by root

Common options:

OptionPurpose
-name PATTERNMatch by filename (case-sensitive)
-iname PATTERNMatch by filename (case-insensitive)
-type f / d / lFile, directory, or symlink
-user USEROwned by a specific user
-group GROUPOwned by a specific group
-perm MODEMatch specific permissions
-mtime NModified N days ago
-atime NAccessed N days ago
-size NMatch specific size
-maxdepth NLimit 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: find with -exec rm or -delete is 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

CommandPurpose
sudo updatedbUpdate the database
locate FILEFind a file by name
locate -n N FILELimit to N results
locate -i FILECase-insensitive
locate -c FILECount matches
locate -r REGEXRegex match
locate -q FILEQuiet mode

find โ€” Name & Type

CommandPurpose
find . -name FILEBy name
find . -iname FILECase-insensitive
find . -type fFiles only
find . -type dDirectories only
find . -type lSymlinks only
find . -maxdepth NLimit depth

find โ€” Ownership & Permissions

CommandPurpose
find . -user USERBy owner
find . -group GROUPBy group
find . -perm MODEBy permissions

find โ€” Time

CommandPurpose
find . -mtime -NModified in last N days
find . -mtime +NModified more than N days ago
find . -atime -NAccessed in last N days
find . -newer FILENewer than another file

find โ€” Size

CommandPurpose
find . -size +100MLarger than 100 MB
find . -size -1MSmaller than 1 MB
find . -size 10kExactly 10 KB
find . -emptyEmpty files/dirs

find โ€” Action

CommandPurpose
find . -exec CMD {} \;Run command on each result
find . -exec CMD {} +Batch command (faster)
find . -ok CMD {} \;Ask before each
find . -deleteDelete matches
find . -printPrint (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

PitfallProblemSolution
locate finds old filesStale DBsudo updatedb
locate finds deleted filesStale DBsudo updatedb
find misses a fileWrong pathCheck starting dir
Wildcards expanded by shellWrong resultsQuote them: '*.txt'
find / slow + noisyScanning everythingLimit path and use 2>/dev/null
-mtime -7 misreadMeans “within 7 days”+7 = older than 7 days
-size +100M no matchCase-sensitive unitUse M, k, G correctly
-exec runs per fileSlow on many filesUse {} + 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

CommandPurposeExample
sudo updatedbUpdate locate DBsudo updatedb
locate FILEFind by namelocate aaa.txt
locate -n N FILELimit resultslocate *.txt -n 5
locate -i FILECase-insensitivelocate *.TXT -i
locate -c FILECount matcheslocate *.TXT -c
find . -name FILEFind by namefind . -name aaa.txt
find . -iname FILECase-insensitivefind . -iname aaa.txt
find . -type dDirectories onlyfind . -type d -name dir1
find . -type fFiles onlyfind . -type f -name '*.txt'
find . -user USERBy ownerfind . -user root
find . -group GROUPBy groupfind . -group sudo
find . -perm MODEBy permissionsfind . -perm 644
find . -mtime -NModified recentlyfind . -mtime -7
find . -atime -NAccessed recentlyfind . -atime -1
find . -size +NBy sizefind . -size +100M
find . -emptyEmpty files/dirsfind . -empty
find . -maxdepth NLimit depthfind . -maxdepth 2
find . -exec CMD {} \;Run commandfind . -name '*.tmp' -exec rm {} \;
find . -deleteDelete matchesfind . -name '*.bak' -delete

Key takeaways:

  • locate is fast because it reads a pre-built database โ€” but the results can be stale
  • sudo updatedb refreshes the locate database
  • locate only matches on filename โ€” no size, owner, or time filters
  • find walks 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 N keeps find from scanning huge trees
  • Always dry run before combining find with -delete or -exec rm
  • On modern systems, find piped to xargs -0 or used with -print0 handles 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!