Searching for Files in Linux: Essential Commands and Techniques

Quickly finding files in Linux is an essential skill for system administration and daily use. Here are several of the most commonly used and effective methods, starting with the simplest and fastest.

1. <span>locate</span> command – The simplest and fastest method

<span>locate</span> is fast because it does not search the disk in real-time but queries a pre-built file database (usually located at <span>/var/lib/mlocate/mlocate.db</span>).

How it works: The system automatically updates this database daily through a scheduled task (usually <span>cron</span>). This means it may not find newly created or modified files because the database has not yet been updated.

Installation (if not already on the system):

  • Ubuntu/Debian: <span>sudo apt install mlocate</span>

  • CentOS/RHEL/Fedora: <span>sudo yum install mlocate</span> or <span>sudo dnf install mlocate</span>

After installation, you need to manually update the database first (as it is empty right after installation):

sudo updatedb

Basic usage:

# Find all files and paths containing 'filename'locate filename
# Use wildcards for more precise matchinglocate *.jpg       # Find all .jpg fileslocate *report*    # Find files with 'report' in the name
# Ignore case (-i)locate -i myfile   # Will find MyFile, MYFILE, myfile, etc.
# Count the number found (-c)locate -c *.log    # Count how many .log files are found

Advantages: Unmatched speed.

Disadvantages: Results are not real-time.

2. <span>find</span> command – The most powerful and precise method

<span>find</span> is the “Swiss Army knife” for finding files, with extremely powerful functionality. It traverses the specified directory in real-time, so the speed depends on the size of the directory structure, but the results are absolutely accurate.

Basic syntax:

<span><span>find [path] [options] [actions]</span></span>

Common examples:

Find by filename:

# Find a file named 'test.txt' exactly in the current directory and subdirectoriesfind . -name "test.txt"
# Use wildcards to find all .txt filesfind /home -name "*.txt"
# Ignore case (-iname)find . -iname "myfile*" 

Find by type (<span>-type</span>):

find . -type f           # Find all regular files (f)find /var -type d        # Find all directories under /var (d)find . -type l           # Find all symbolic link files (l)

Find by time:

# Find files modified in the last 60 minutes (-amin, -mmin)find . -mmin -60        # Modified in the last 60 minutesfind . -mmin +120       # Modified more than 120 minutes ago
# Find files modified in the last 7 days (-atime, -mtime)find /home -mtime -7    # Modified in the last 7 days

Find by size (<span>-size</span>):

find . -size +10M       # Find files larger than 10MBfind . -size -50k       # Find files smaller than 50KBfind . -size +100M -size -1G # Find files larger than 100MB but smaller than 1GB

Execute actions after finding (<span>-exec</span>):

# Find all .txt files and delete them (very dangerous, test first!)find . -name "*.txt" -exec rm {} \;
# Find all .jpg files and copy them to /backup directoryfind . -name "*.jpg" -exec cp {} /backup \;

Advantages: Comprehensive functionality, flexible condition combinations, real-time accurate results.

Disadvantages: Slower in large file systems.

3. Other specialized commands

<span>which</span>: Used to find the location of <span>executable files</span> in the directories set in the environment variable <span>$PATH</span>.

which python    # Output might be /usr/bin/pythonwhich ls

<span>whereis</span>: Not only finds executable files but also the locations of their source code and manual pages.

whereis python# Output: python: /usr/bin/python /usr/lib/python2.7 /usr/share/man/man1/python.1.gz

4. Modern alternatives:<span>fd</span> command

<span>fd</span> is a user-friendly <span>find</span> alternative written in Rust. It provides colored output, ignores hidden files and files in .gitignore by default, and searches faster.

Installation:

  • Ubuntu/Debian: <span>sudo apt install fd-find</span>

  • CentOS/RHEL/Fedora: <span>sudo dnf install fd-find</span>

  • It can also be installed using <span>cargo install fd-find</span>

Basic usage (similar to <span>find</span> but more concise):

fd pattern      # Recursively find files containing 'pattern'fd -e jpg      # Find all .jpg filesfd -H '^config' # Find files starting with 'config' (-H includes hidden files)

Advantages: Out of the box, reasonable defaults, fast speed, simple syntax.

Summary Overview

Tool Features Use Cases Speed
<span>locate</span> Database-based, simple and fast Quick fuzzy search by filename Extremely fast
<span>find</span> Most powerful, rich conditions Precise search by various attributes (type, time, size, etc.) Slower (real-time traversal)
<span>which</span> Finds the absolute path of executable files. To find out where a command’s program file is Fast
<span>whereis</span> Finds executable files, source code, and manual pages To find all files related to a command Fast
<span>fd</span> <span>find</span> modernized alternative, user-friendly defaults Fast recursive file search, concise syntax Fast

How to choose?

  • Just want to quickly find a file by name, regardless of whether it’s the latest?

    -> Use <span>locate</span>

  • Need to search precisely based on complex conditions (time, size, type, etc.), or handle newly created files?

    -> Use <span>find</span>

  • Just want to find where a command program is located?

    -> Use <span>which</span> or <span>whereis</span>

  • Frequently need to recursively search for files in the terminal and want a better experience and speed?

    -> Install and use <span>fd</span>

Leave a Comment