How to search for files in terminal on Linux

How to search for files in terminal on Linux

Although there are many ways to search and locate files and directories on Linux, the easiest and fastest method is probably through the terminal. However, few Linux users are aware of this, which can lead to unnecessary frustration. This is a quick guide, hope to help you find what you are looking for in your system.

Use positioning commands

Positioning“Command is the preferred method for Linux users because it is much faster than other commands. The reason for this unparalleled speed is that the locate command is not actually searching the local hard disk for the file or directory you need to find, and It is more like reading the mlocate.db database file containing all the file paths in the system.

If locate is not installed, use the following command:

Debian and Ubuntu

sudo apt-get install locate

CentOS

yum install locate

Prepare to use positioning commands for the first time

To update the mlocate.db database before first use, run:

sudo updatedb

To use locate, open a terminal and type locate, followed by the file name you are looking for. In this example, I want to search for files that have the word “sunny” in their name.

locate sunny

Targeting can also tell you the number of search keyword matches in your database. This can be achieved by including the “-c” parameter in the command, which stands for “count”.

locate -c sunny

How many times a keyword has been matched using targeting

What users need to keep in mind here is that “locate” requires the exact name of the file you are looking for, as opposed to other more flexible tools. This means locate is great for finding what you are sure about its name, but not for situations where you don’t fully remember the filename.

locate howtoforge

versus

locate Howtoforge

Find exact file name needed

And, because “Positioning“Reading a database file, the results may be outdated and not entirely correct. To alleviate this problem, you can type”Sudo update“On a terminal session.

See more examples of how to use the locate command here.

If you still don’t find what you want after that, or you are just overwhelmed by the number of results, continue to the next step, the Find command.

Use the find command

FindCommand is a powerful but slow search utility. This is because, contrary to the “locate” command, “find” actually searches the disk for files and directories that the user needs. Find is great when you’re trying to find a file or directory but don’t remember its name, because Find can search for files that belong to a user or a group of users, files that have been modified or accessed. Recently, files with a specific size range, hidden files, executable files, read-only files, and files with specific permissions. The best part is that users are free to combine multiple of these conditions in one “find” command, which substantially reduces the scope of the results.

When it comes to narrowing down, the first thing to do when running Find is to tell it to search in a specific directory. This will significantly speed up the search process, but always depends on the size of the directory. If you know where the file might be, open a terminal, navigate to the directory and run “Find . [filename]“. The dot tells find to search on the current directory. If you want to search the home directory, replace the dot with” ~ / “, if you want to search the entire file system, use” / “.

For example, I want to search for files that contain posters in my download directory. I know the word “poster” in the file name, but I don’t exactly remember the name. Therefore, I will browse to my download folder through a terminal with “cd Downloads” and enter the command:

sudo find . -name "*poster*"

The results are as follows:

Using the find command on Linux

This tells me that there is a pdf file called “billy_poster copy” in the “Downloads” folder. If I don’t know where it is, if I search in the parent directory, Find still tells me it’s in the Downloads folder.

Search for files found by file name

Now if you replace the “-name” parameter with “-iname”, you can get the result regardless of the case of the letters. That’s something not found in “locate”, so another useful element when you’re unsure of the file name.

sudo find . -iname "*poster*"

Use the -iname option in the find command

Now, let’s assume I’m still searching for the same poster file, and the only thing I remember is that it’s less than 5 MB in size. In this case, the command I will use is:

find ~/ -size -5M

If I knew it was taller than 2MB, then the command would be “find ~ / -size + 2M”. The best part is that find supports Boolean operators to make searching more powerful. In this example, I will use a command that combines my knowledge that the size of the file is less than 5 megabytes and also greater than 2. This command is:

find / -size -5M -and -size +2M

Find files by size

Finally, it is recommended that we remember nothing but remember to visit it three minutes ago. To find it, use the following find command:

find / -amin -3

It would be “find / -amin -30” for half an hour, “-amin -120” for two hours and so on. If you accessed the file two days ago, use this command instead:

find / -time -2

This will show all files accessed in the search location in the past two days.

Find files by timestamp

I think the above covers most common use case scenarios. For more information on the powerful Find command, open a terminal and enter “man find”. Good luck finding what you need.

More examples of the Linux find command can be found here.

Sidebar