Finding Files in Linux

There are several ways to find files in Linux when you don’t know their exact location. This guide covers the most common tools and adds useful tips and examples.


1. find

If you have a rough idea of the directory tree where the file might be, you can use find:

find directory -name targetfile -print

For example:

find /home -name "*.txt" -print 2>/dev/null
  • *.txt → matches all text files
  • 2>/dev/null → suppresses permission error messages
  • Quotes (") → prevent the shell from expanding the *

Useful Options

  • By type:
    find . -type f → only files
    find . -type d → only directories

  • By permissions:
    find . -perm o=r → files readable by others

  • By size:
    find . -size +10M → files larger than 10 MB

  • Execute commands on matches:

    find . -name "*.txt" -exec wc -l '{}' ';'

    Counts the lines in every .txt file.

  • Case-insensitive search:

    find . -iname "*.jpg"

📌 For more details: man find or info find


2. which (sometimes whence)

If you can run a command, you can locate its binary on disk:

> which ls
/bin/ls

Useful Alternatives

  • type ls → shows if it’s a built-in, alias, or binary
  • command -v ls → POSIX-compliant alternative to which

3. locate

locate uses a prebuilt index of filenames, making it much faster than find.

Example:

locate ".txt"

This will list all files with .txt in their path.

Notes

  • The index is usually updated daily. To refresh it manually:

    sudo updatedb
  • May list files that have been deleted or miss very new files.

  • Unlike find, it cannot filter by permissions, size, or type.


Comparison

CommandSpeedFreshness of resultsSearch criteriaExample use case
findSlowAlways up-to-dateFlexible (name, size, type, perms, exec)General-purpose searching
whichFastAlways up-to-dateExecutables onlyFind binary locations
locateVery fastMay be outdatedFilename patterns onlyQuickly list many matches

Other Useful Tools

  • whereis <command> → shows binary, source, and man page location
  • fd <pattern> → a modern, faster alternative to find (if installed)
  • grep -rl <pattern> <dir> → find files containing a text string