How to find a file in Linux
There are many ways to search for files on a Linux system. Each comes with its pros and cons. But for best results, it is good to know as many of them as you can.
Graphical programs often come with integrated search, and while they can be convenient, they often have their limits. That is where shell search tools come in handy, as they have more features to offer.
The following list shows you the most common ways to search for a file in Linux and includes the syntax and usage patterns for each of the programs.
You should note that only the first command works on graphical programs, so the rest are shell programs.
Linux Search Methods
Command | Description | |
---|---|---|
1. | CTRL+F | Calls up the search feature in most GUI programs |
2. | find | Extensive search tool with powerful features |
3. | locate | Fast database-driven search tool |
4. | grep | Powerful regex-based search tool |
5. | which | Used to search for executable files |
6. | whereis | Searches executable files, their sources, & man page files |
1. CTRL+F
Ctrl+F is a standard feature on most graphical programs to search for files on the system. It works on the Gnome file manager Nautilus, the KDE file manager Dolphin, and Thunar, the Xfce file manager.
It also works on many other programs such as Evince for reading .pdf files, the Firefox browser, office programs like Libre Office, and many more.
Usage is simple. While using the program and you need to search for a file, press the Ctrl and the F keys on your keyboard together, and a search bar opens up on the program.
2. Find
When it comes to searching for stuff on the Linux platform, find is the grand-daddy of all the tools out there. It is a versatile and very powerful program that takes a wide range of options to make it work in extensively different styles.
There are so many options, operators, and printing formats for the find program that it is impossible to simply read and know them all. You will need to constantly read the man and info pages if you want to learn the find program. Here are some example and what they do:
man find > #read man pages find entries
info find > #read info pages find entries
The general syntax for find is as follows:
find [options] [path…] [expression]
Where options refer to its many options, the path is where you want to start searching from, and the expression includes the many search patterns and operators that are available for use.
There are, however, a few options you can learn and memorize quickly, and they are as follows:
a) -type
This searches for a file based on its type and it can include “f” for a regular file, “d” for a directory, and “l” for a symbolic link.
find -type f -name ‘*.js’ #find all JavaScript files in the user’s home
b) -name
As shown above, this searches for the occurrence of the given pattern (*.js) in the file’s name.
c) -mtime
With the -mtime option, you specify time-based search criteria, such as
find -mtime 7 #find all files that were modified in the last week (7 days)
d) -size
You can also use the file’s size as search criteria, such as
find -size +15k > #find all files in the home directory larger than 15 kilobytes
Note that all the options’ arguments can take – or + numbers to show less than or above. For example -2 for less than 2 and +15 for more than 15. There are so many more options and you should read both the “man find” and “info find” entries on your shell to get a hang of this powerful program.
3. Locate
The locate command is another impressive search tool on Linux. It is not as feature-rich as the find command, but it is very much faster because it uses database entries to perform its magic.
Locate is also easier to use and does not need you to enter the search path. Just enter locate, followed by a pattern for the file’s name and it will return all files that meet that pattern. For example:
locate moz > #find all files that contain ‘moz’ in their name, such as mozilla
Locate also offers a few options, such as -i or –ignore-case as it is otherwise a case-sensitive search tool. For instance:
locate -i moz > #find both mozilla, MOZILLA, & Mozilla named files
4. Grep
Sometimes you may know some of the words contained in a file. So, while the find and locate commands may not be suited for finding such a file, the grep command, however, is perfect for the job.
Grep stands for Global Regular Expression Print. It accepts regex as arguments and can also format its output based on your preferences. It can color the matched words, print their line numbers, or only print the name of the matched file.
Keep in mind that grep matches both the file name and the file contents, and you are also free to supply patterns for each. The grep syntax goes like this:
grep [options] ‘pattern’ file
A lot of options are available and you can combine them as you see fit, but you will get the most out of grep if you learn regex (regular expressions) because they can be really helpful. The info and man pages also do a very good job explaining this.
Here are few grep examples and what they do:
grep ‘foobar’ > #find all files that contain foobar in the current directory
grep -in ‘foobar’ > #be case insensitive and print line numbers
grep -inr ‘foobar’ /home/user > #recursively search all folders under /home/user
grep -ir ‘table’ *.html > #find all html files that contain a table in the home directory
5. Which
If, for some reason, you need to know the absolute path of an executable file in Linux, use the which command. It works like this:
which grep > #returns /bin/grep on Ubuntu as grep’s absolute path
which which > #returns /usr/bin/which
6. Whereis
Whereis is a bit like which, but it returns binary, source, and manual page files for any given command. Here is an example:
whereis grep > #returns only binary & man files /bin/grep /usr/share/man/man1/grep.1.gz
Conclusion
We have reached the end of our how to find a file in Linux commands list. As you can see there is a wide range of tools and options in the shell. So there is something for everyone.
Just take your time to learn and practice with these tools and remember to read their info and man pages as well. Before long, you will become very efficient with file searches on Linux.