How Do You Search for a File or Directory in Linux?

1 min read

Finding files and directories efficiently is a key skill for any Linux or Unix user. Whether you’re tracking down a misplaced document or hunting for a configuration file, the terminal offers powerful tools for searching.

Using the find Command

The find command is the most versatile way to search for files and directories.

Basic Usage:

find [path] [options] [expression]
  • [path] is where to start searching (e.g., /home/user/).
  • [options] and [expression] specify what to look for.

Examples:

  • Find a file named notes.txt in your home directory:

    find ~/ -name "notes.txt"
    
  • Find all .log files in /var/log:

    find /var/log -type f -name "*.log"
    
  • Find all directories named backup:

    find / -type d -name "backup"
    

Common Options:

  • -type f : Files only
  • -type d : Directories only
  • -iname : Case-insensitive name search
  • -size +10M : Files larger than 10MB

Using the locate Command

The locate command is much faster than find because it searches a pre-built database.

  • Find all files containing config in their name:
    locate config
    

Note:
Run sudo updatedb periodically to update the database.

Using the which and whereis Commands

  • which shows the location of an executable:

    which python
    
  • whereis locates the binary, source, and man page:

    whereis gcc
    

Graphical Search Tools

Most desktop environments offer graphical search tools (like GNOME’s “Files” or KDE’s “Dolphin”) with search features built-in.

Tips for Efficient Searching

  • Use wildcards (*) to match patterns.
  • Combine commands with grep for advanced filtering:

    find ~/Documents -type f | grep "report"
    
  • Limit search depth with -maxdepth:

    find /etc -maxdepth 2 -name "*.conf"
    

Summary:
To search for files or directories, use find for flexibility and locate for speed. Mastering these commands will save you time and frustration when navigating your system.

You Might Also Like