πŸ›‘οΈResearcher Sheru!

Your Image Badge

πŸ“… Published on: June 28, 2025 ✍️ Author: Himangshu Pan

πŸ“ Searching for Files in the Linux Terminal

Just like we rely on Google to search the web, we often need similar tools to locate files within our own system β€” especially when we forget where they’re stored. On Linux, where everything is treated as a file (even devices and processes), having effective file search tools is crucial.

In this guide, we’ll explore three essential commands that make file searching efficient on any Linux-based system: which, locate, and find.


πŸ” which – Locate Executable Paths

The which command identifies the full path of the executable file that will run when a command is entered. It looks through the directories listed in your $PATH environment variable and returns the first matching executable.

πŸ§ͺ Example

To find the full path of the ls command:

which ls

This will output something like:

/bin/ls

This means when you type ls, the system actually runs /bin/ls.


πŸ” Finding All Executables

To list all instances of a command in your system’s $PATH, use the -a flag:

which -a ls

This might return:

/bin/ls
/usr/bin/ls

The system chooses the first path listed in $PATH. To view the current search order:

echo $PATH

Directories listed earlier in $PATH take priority, so /bin/ls is used before /usr/bin/ls if /bin appears first.


πŸ“š locate – Fast File Search Using a Database

The locate command quickly finds files by searching a prebuilt index (database) of all file paths on the system. Since it doesn’t scan in real time, it’s much faster than find for basic lookups.


βš™οΈ How It Works

Instead of scanning the entire filesystem, locate queries a cached database (updated via the updatedb command, often run automatically by cron).

To search for a file:

locate passwd

This might return results like:

/etc/passwd
/var/backups/passwd.bak

πŸ” Update the Database

If you’ve recently added or removed files, refresh the database manually:

sudo updatedb

πŸ”§ Note: On some systems, you may need to install mlocate:

sudo apt install mlocate

Combine locate with grep for case-insensitive filtering:

locate passwd | grep -i root

πŸ“„ Practical Example

Let’s say you downloaded a PDF and forgot its location. Just run:

locate *.pdf

And get a list of all .pdf files instantly.


The find command performs a live scan of the filesystem and supports a wide variety of filters and actions, making it one of the most powerful search utilities available.


πŸ“Œ Basic Syntax

find <path> <options> <expression>

πŸ” Common Examples

πŸ”  Search by Name

find / -name passwd

Case-insensitive:

find / -iname passwd

πŸ“ Search by File Type

Only directories:

find / -type d -name config

Only regular files:

find / -type f -name filename.txt

🧱 Search by Size

Files larger than 100MB:

find / -type f -size +100M

Files smaller than 10KB:

find / -type f -size -10k

βš™οΈ Run Commands on Results

Delete all .tmp files in /tmp:

find /tmp -type f -name "*.tmp" -exec rm {} \;

List details for all files in the current directory:

find . -type f -exec ls -l {} \;

🚫 Exclude Directories

Search from root but skip /proc:

find / -path /proc -prune -o -name passwd -print

πŸ•’ Search by Time

Modified within last 7 days:

find / -type f -mtime -7

Accessed within last 24 hours:

find / -type f -atime -1

βœ… Final Thoughts

Each tool has its strengths:

Mastering these commands will significantly improve your efficiency when navigating or troubleshooting a Linux system.

Happy searching! 🐧



Blogs β†’