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:

Terminal
$find . -name 'config.json'
./project/config.json ./backup/old/config.json

Syntax: find [where] [criteria] [action]

Basic Usage

Terminal
$find /home -name '*.txt'
(all .txt files in /home)
$find . -name 'readme*'
(files starting with readme)

Case-Insensitive

Terminal
$find . -iname 'readme.md'
./README.md ./docs/Readme.md

-iname ignores case.

By Type

Terminal
$find . -type f -name '*.js'
(only files, not directories)
$find . -type d -name 'config'
(only directories named config)

Type options:

  • f = regular file
  • d = directory
  • l = symbolic link

By Size

Terminal
$find . -size +100M
(files larger than 100MB)
$find . -size -1k
(files smaller than 1KB)

Size suffixes: c (bytes), k (KB), M (MB), G (GB)

By Time

Terminal
$find . -mtime -7
(modified in last 7 days)
$find . -mtime +30
(modified more than 30 days ago)

Time Units

-mtime uses days. For minutes, use -mmin:

hljs bash
find . -mmin -60    # Modified in last hour

Combining Criteria

Terminal
$find . -name '*.log' -size +1M
(log files over 1MB)
$find . -type f -name '*.tmp' -mtime +7
(temp files older than a week)

find with Actions

Delete Found Files

Terminal
$find . -name '*.tmp' -delete
(deletes all .tmp files)

Dangerous

-delete is permanent. Always run without -delete first to see what would be deleted:

hljs bash
find . -name '*.tmp'          # Preview
find . -name '*.tmp' -delete  # Execute

Execute Command

Terminal
$find . -name '*.sh' -exec chmod +x {} \;
(make all .sh files executable)

{} is replaced with each found file. \; ends the command.

Modern Alternative: fd

fd is a faster, more user-friendly alternative to find:

hljs bash
# Install it
sudo apt install fd-find  # Then use: fdfind (or fd on other distros)
Terminal
$fd config
src/config.js config/settings.json
$fd -e js
(all .js files)
$fd -t d node_modules
(only directories)

fd is simpler: patterns don't need quoting, it ignores .git and hidden files by default, and has colorized output.

When to Use Which

  • fd for interactive searching - faster, simpler syntax
  • find for scripts - available everywhere, more powerful options
  • locate for system-wide searches - fastest, but might be stale

locate - Faster, But Less Current

locate uses a pre-built database - much faster than find:

Terminal
$locate nginx.conf
/etc/nginx/nginx.conf /usr/share/doc/nginx/nginx.conf.example

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

Featurefindlocate
SpeedSlow (searches live)Fast (uses database)
FreshnessAlways currentCan be stale
CriteriaMany optionsName only
ActionsCan execute commandsSearch only

which - Find Executables

Find where a command lives:

Terminal
$which python3
/usr/bin/python3
$which node
/usr/local/bin/node

Find binary, source, and man page:

Terminal
$whereis bash
bash: /usr/bin/bash /usr/share/man/man1/bash.1.gz
Knowledge Check

Which command finds files modified in the last 24 hours?

Key Takeaways

  • find searches live - always current but slower
  • locate uses a database - faster but may be stale
  • find . -name 'pattern' is the basic search
  • Add -type f for files only, -type d for directories
  • Use -size, -mtime to filter by size and age
  • Preview before using -delete or -exec

Congratulations! You've completed Chapter 3: Working with Files.

Next chapter: Text Manipulation - grep, cut, sort, and the powerful tools that process text.