Most users trying to find files and folders on their Linux PC resort to the file manager. Usually, file manager search tools can find what users need — to a degree. The problem is, they tend to be slow and finicky. For more powerful file searching on Linux, consider turning to the terminal.
SPOILER ALERT: Scroll down and watch the video tutorial at the end of this article.
In this guide, we’ll go over a few easy ways to find files and folders quickly, and efficiently with the Linux terminal.
1. Find And Grep
Find is a powerful search tool for the Linux desktop. It works on the command line by running search queries based on keywords that the user specifies. Find is very complex, and as a result many use it. Here’s how to use find. First, open up a terminal and run find. You’ll notice that find lists out pretty much every file on your PC. It’s good that find looks through so much, but the problem with running it like this is that you’ll never be able to find what you’re looking for.
To get better use out of the find tool, consider chaining it together with a few commands. Specifically, grep. Grep is also very powerful, and with it, users can filter out specific keywords. Combining this with find is an awesome combo. For example, to find files and folders labeled “bitcoin” files on your Linux PC, you’d do:
find | grep bitcoin
Grep filters out find and only shows us the results that have the “bitcoin” keyword in it.
It also works with specific file extensions. For example, to find every PNG file on your Linux PC, run find and grep like this:
find | grep .png
There are probably a ton of PNG files on your Linux PC, so to make the list more user-friendly, pipe in more as a third command.
find | grep .png | more
Mlocate is another very useful command. Arguably, it is more user-friendly than find. The reason that locate works better, is that it can search better with keywords. There is no need to use extra command options, or things like grep to carry out this. Unfortunately, the locate tool isn’t pre-installed on every Linux distribution. Here’s how to set it up. First, install the software on your PC.
sudo apt install mlocate
sudo apt-get install mlocate
sudo pacman -S mlocate
sudo dnf install mlocate
sudo zypper install mlocate
With the software installed, open up the terminal and use the updatedb command to create a new database. Creating a new database is required, or the locate command will error out, and refuse to work. Keep in mind that a database scans ALL FILES on your PC, so it must be run as root, and not a regular user command.
The updatedb command takes quite a long time depending on your hard drive’s speed, as well as the number of files on the system. Keep the terminal open and let the command run. You’ll know the database is ready to go once the terminal accepts text entry again.
To search using the locate tool, follow the example below. Be sure to change “filename” or “keyword” to your own search terms, as these words are just placeholders.
After entering a search term, locate will check the database and return results for you. If there are a lot of things to sort through, consider piping the results to a text file for later.
locate keyword >> locate-keyword-results.txt
Filter Mlocate Text File
Piping a search result to a text file is a good way to keep a backlog of search results for future use. If you need to filter through these locate search results, try using cat and the grep command together.
For example, to filter through a past search result run to locate all MP3 files on my PC, the command is:
cat locate-mp3-results.txt | grep "mp3 keyword"
Grep will filter out the exact file you need.
3. LS And Grep
Aside from using a search tool to crawl through your Linux PC, there are other ways to look for files. The main way to do this in the terminal is with the ls command (aka List). This tool is a simple one but lists all items in a directory. It can be very useful if you need to find a specific folder. However, ls on its own can be terribly inefficient. To improve this command, consider combining it with grep. Doing this will make it easier to filter out the folders or files you’re looking for. Or at the very least, filter it down to a few keywords.
First, use cd to move to a directory where you’re searching for specific files or folders.
Then, combine ls and grep.
ls | grep keyword
Alternatively, have ls reveal hidden folders too, with:
ls -a | grep keyword