How To Locate A File In Linux

How To Locate A File In Linux

Reading time1 min
#Linux#Commands#Search#find#locate#fd

Mastering File Location in Linux: Beyond Basic Commands

Every Linux engineer has faced it—a process locked by a runaway log, a missing binary in $PATH, or a rogue config tucked several layers deep. Files scatter, trees grow dense. Precision file location isn’t just about knowing find and locate; it’s about using the right tool, with the right parameters, right now.


File Search Is a Performance Bottleneck

Default tooling (find, locate) suffices for casual use:

find /srv -name backup.tar.gz
locate nginx.conf

But scale up—say, /home with ~1M files, or a CI workspace with layered vendor directories—and search quickly becomes a performance bottleneck. Worse: a stale locate database can mislead during incident response.


find Tightened: Depth, Type, and Permissions

Speed comes not from “faster hardware,” but from surgical queries. Consider:

Search by type:

File TypeOption
Regular file-type f
Directory-type d
Symlink-type l

Typical pattern when tracking executables in /usr/local/bin:

find /usr/local/bin -type f -perm /u=x
  • -perm /u=x: Files the owner can execute (accommodates SUID/SGID).

Gotcha: Omitting -type f adds noise from directories or pipes.

Limit recursion:

Large hierarchies? Cap with -maxdepth:

find /opt/apps -type d -maxdepth 2 -name "cache"

Handy when deep vendor directories choke recursive scans.

Combine logic:

Locate files touched in the last week over 50MB—logs, DB dumps, or surprise core files:

find /data -mtime -7 -size +50M

Mix criteria:

find /var -name "*.log" -o -name "*.out" -a -mtime -2

Beware grouping (\(, \))—misplaced logic here yields unexpected sets.


Mass Actions: -exec With Care

Cleanup of temporary artifacts:

find /tmp -type f -name '*.tmp' -exec rm -f {} +
  • The trailing + batches calls, reducing fork overhead (\; starts a new process per match).
  • For dangerous deletions, begin with ls -lh to audit matches.

Race condition warning: Files created between find search and rm execution can slip in or out. Always check with -print prior to destructive actions.


Beyond find: Faster Alternatives

fd—Modern Search for Dev Workflows

fd (e.g., fd-find v8.7.0) provides colorized, parallelized search—useful inside project trees:

fdfind -e rspec

Ignores .gitignore by default, keeping results relevant for codebases.

Installation (Debian/Ubuntu):

sudo apt install fd-find
alias fd=fdfind  # Optional: unify name

Known issue: The command may be aliased differently across distributions.

ripgrep (rg)—Filename and Content in One

rg outpaces both grep and find for text-sensitive discovery:

  • By content:
    rg 'API_KEY=' /etc/secrets/
    
  • By file name (non-obvious tip):
    rg --files | rg requirements
    
  • Excludes git-ignored files, no extra flags.

locate—Blazing Fast, But Not Real-Time

locate pulls from an indexed database (/var/lib/mlocate/mlocate.db), potentially hours out-of-date. Update after major file operations:

sudo updatedb

Targeting a project tree:

locate settings.py | grep '^/srv/prod/'

Trade-off: Real-time accuracy versus speed. For ephemeral files, avoid.


Content Search: grep -rl, ack, and Binary Gotchas

Name insufficient? Find content:

grep -rl "search_term" /mnt/shared/
  • -r: Recursively traverse dirs.
  • -l: Output matching filenames only.

Binary files or packed logs? Prefer ack or rg, as grep may bail with “Binary file ... matches”.


Applied Example: Large, Recently Changed Logs

Reducing /var/log bloat by finding huge, fresh logs worth archiving:

find /var/log -type f -name '*.log' -mtime -2 -size +200M -exec ls -lh {} +
  • Easy to funnel into compression, deletion, or rsync for archiving.

Best Practices

  • Limit search scope early. Don’t waste CPU traversing /proc, /sys, or NFS mounts unless required (-xdev is your friend).
  • Use explicit type and name filters. Avoid “needle in a haystack” queries.
  • Integrate faster tools where appropriate. For dev work: fd, ripgrep. For system-wide audits: careful find.
  • Validate before deletion. Scan output visually when using -exec rm.
  • Watch for symlink loops and permission denials. Add 2>/dev/null as needed; sometimes surface the errors to understand why results are missing.

Side Note: Locate Hidden Files or Dotfiles

Standard find ignores hidden files unless you broaden patterns:

find . -name ".*rc"

Not all tools handle dotfiles identically. Check with each upgrade, as default globs might change.


File location in Linux remains a core diagnostic and maintenance skill. The practical know-how is less about memorizing all flags, and more about recognizing which search, pattern, and scope fits your scenario—and knowing the side effects. The minute saved per search operation compounds, especially in large fleet environments.

For lesser-discussed alternatives (e.g., plocate, entr for watching file changes), see their respective man pages—or better, test in a sandbox VM first. No single approach is flawless. Context dictates the method.