Introduction
There are five file searching commands on Linux: whereis
, locate
, which
, type
, and find
. Each of them have their own characteristics and is designed for particular search scenarios. This guide will introduce you to each command and show example usages. For further reading on any of these commands, it is best to review the appropriate manpage.
whereis
The whereis
command is used to search binary files, source code, and the online manual pages at several standard install directories for any program name specified.
Because whereis
does not search every location on your system, any files out of these specific directories will not be found. For the same reason, you will get your search result quickly, whether found or not.
Also, it will not search for those commands which are built directly into the shell.
For example, if you want to find info about the ls
command, run the following command on your terminal:
whereis ls
You will get some feedback like:
ls: /bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
In the result, /bin/ls
is the binary you want to locate, the other two are manpages for the ls
program, no source code for ls
program was found.
Copy the /bin/ls
file to your home directory and to the /usr/bin
directory, and then run the whereis
command again:
cp /bin/ls ~
cp /bin/ls /usr/bin/ls
whereis ls
As you see, only /bin/ls
and /usr/bin/ls
were found in the search result. The ls
program in your home directory was not found, because your home directory is not one of those standard install directories.
ls: /bin/ls /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
If you run the following command:
whereis cd
You will see output similar to the line below. There was no binary file was discovered because cd
is a built-in command of the shell.
cd: /usr/share/man/man1/cd.1.gz /usr/share/man/man1p/cd.1p.gz
locate
The locate
command is used to find files by name with the help of a database (/var/lib/mlocate/mlocate.db
). This database is basically a snapshot of the layout of your filesystem. Any records in this database that match your query will be listed in the search result. By default, the locate
command will use your query to match any part of the whole name of each record, including the path name. Therefore, the number of matched files in the search result could be more than you expected.
As the database becomes outdated, the search result of the locate
command becomes less accurate. You may notice files that no longer exist, or you won’t see matches for newly-created files. By default, the mlocate.db
database is automatically updated once a day. You can use the updatedb
command to manually update it.
Because the search is performed on the database instead of the filesystem, the search speed is very fast.
If you run the following command:
locate ls
You will get many records instead of your expected result.
In order to make the search result more accurate, you can use the -b
flag to restrict the search range, using your query to match only the basename of each record:
locate -b "/ls"
This time, you will get the location of each file exactly called ls
on your filesystem:
/bin/ls
which
The which
command will search for the command you specified in the value of environment variable PATH
and return the first result by default. If you are querying an alias, the which
command will determine the actual command for the alias before performing your search.
The which
command is also very fast because of its simplicity.
The usage of the which
command is simple:
which your_command
If you want to show all of the matched commands instead of the first one, use the -a
flag:
which -a your_command
Here is an example for searching for a command that has been aliased:
which ll
The search result would be:
alias ll='ls -l --color=auto'
/bin/ls
type
By default, the type
command will indicate how a command name would be interpreted. Possible results include an alias, a keyword, a function, a builtin, or a file. Just like the whereis
command, the type
command will only search in several standard install directories to answer your query.
Some examples for typing different commands:
A shell builtin command:
type cd
cd is a shell builtin
A binary file on the filesystem:
type sudo
sudo is /usr/bin/sudo
An alias:
type ls
ls is aliased to `ls --color=auto'
find
Among the five file searching commands introduced in this tutorial, the find
command is the most powerful one. It is also the slowest one. Unlike the other four commands, the find
command will actually search for your file on the entire filesystem, one i-node by one i-node. With the find
command, you can use sophisticated query criteria to find every file you need, even additionally execute actions on the files that were found.
Search criteria for the find
command is too sophisticated to explain in such a short article, here are a few examples instead.
Basic format of the find
command:
find [path] [option] [action]
To find all files in the working directory and all of its sub-directories:
find
To find a file called aaa
in your home directory and all of its sub-directories:
find ~ -name 'aaa'
To find all of the files in the filesystem that were modified in the last 24 hours:
find / -mtime 0
To find all of the files in the web directory and all of its sub-directories that belong to user nginx
:
find /usr/share/nginx/html/ -user nginx
To find all of the files in the working directory whose permissions are 0744
:
find -perm -0744
To find a file with the name aaa
in the working directory and list its detailed info:
find -name 'aaa' -exec ls -l {} /;
Want to contribute?
You could earn up to $300 by adding new articles
Suggest an update
Request an article