Knowledgebase

How to Search a File in Linux Using a Command?

Have you ever felt like you have lost something important in a messy room?

We understand how frustrating this situation can be!

Searching for a file in Linux can sometimes feel the same way, especially if you don’t know where you saved it.

But don’t worry, Linux has powerful search commands that can help you find files quickly, just like using ‘Ctrl + F’, but in a smarter way!

In this detailed guide, we will show you how to search for a file in Linux using simple commands. Even if you are new to Linux, this guide will help you understand it easily.

Let’s dive in!


Also Read: How to Restart/Reboot a Linux Server by Command?


Know the Basic Syntax of the find Command

Firstly, let’s find out the basic structure of the find command:

find [path] [options] [expression]

Let’s break this down:

[path] – This is the location where you want to start the search (like /home/user or just . for the current directory).

[options] / [expression] – This is the command for what you are searching for (like file name, type, etc.)


Also Read: How to Create a New Group in Linux?


Ways to Use the Find Command (With Examples)

Let’s go through some common and useful ways to search files using find.

Summary Table

Task Command Example

To search the file by name

find /home -name “file.txt”

To search in the current directory

find . -name “file.txt”

To search by extension

find /docs -name “*.pdf”

To search directories

find / -type d -name “folder”

To search large files

find / -size +100M

To search by date

find / -mtime -7

1. Search by File Name

If you are looking to search for a file with a specific name, you have to use the command given below:

find /path/to/search -name “filename”

Example:

find /home/user -name “notes.txt”

This will search for a file named ‘notes.txt’ in the /home/user directory.

2. Search in the Current Directory

If you want to search in the current directory, use the following command below:

find . -name “filename”

Example:

find . -name “photo.jpg”

Here, The . Means the current directory.

➔ You can ignore case sensitivity using -iname instead of -name

Example:

find . -iname “photo.jpg”

This command will find ‘Photo.jpg’, ‘PHOTO.JPG’, etc.

3. Search for All Files with a Specific Extension

If you want to search for the file with a specific extension, use the following command below:

find /path -name “*.txt”

Example:

find /home/user/Documents -name “*.pdf”

This command lists all PDF files within the Documents folder.

4. Search by File Type

If you want to search for the file with a specific type, use the following command below:

There are two ways for different files:

f → for regular files

d → for directories

Example:

find /home/user -type d -name “project”

By this command, you will find all directories named “project”.

5. Search Files by Size

If you want to search for the file with a specific size, use the following command below:

find /path -size [+/-]size

Here, + means ‘greater than’ and means ‘less than’ the given size.

Examples:

find / -size +10M

It will help you find files larger than 10 MB.

find / -size -1k

It will help you find files smaller than 1 KB.

6. Find Files Modified Recently

find /path -mtime -n

Here,

-mtime -7 refers to the file which is modified in the last 7 days

-mtime +30 refers to the file which is modified more than 30 days ago

Example:

find /var/log -mtime -2

This finds files modified within the last 2 days inside /var/log.


Also Read: Know About the ls Command in Linux With Examples


Conclusion

So next time, when you are scratching your head thinking,

‘Where did I save that file?’,

Just take a deep breath and let the find command do the hunting for you.

Whether it’s a .txt file from last month, a big video taking up space, or a random folder you forgot about, now you have all the skills to track it down like a pro.

Just play around with the examples we shared. Try searching by name, size, or even modification date. It’s kinda fun once you get the hang of it!