Hello, I’m Liang Xu.

Finding files on your system is a skill every engineer must have (whether you’re running Windows, Linux, or MacOS). On Linux, a single find command can do a lot of searching.

However, there are more file search commands than just one find command. This article on Linux under the file search command for a popular science, so that you can find the file you need in a short time.

1. find

The find command is probably the most classic command. When it comes to search tools, the first thing that comes to mind is the find command. However, the find command is so powerful that it would take several articles to cover all of its features.

So, here is a little lazy, introduce the most basic, according to the name of the file to find a file method. If we wanted to search for all.sh files in the current directory (and subdirectories), we could do this:

2. locate

Locate is another command that searches for files by filename. Different from the find command, the locate command does not need to specify a path.

Instead of searching for files in various corners of the system, this command searches in a database called mlocate.db. This database is located in the/var/lib/mlocate/mlocate db, it contains all the files in the system of index, and at the time of every day in the morning by the cron facility automatically update again.

Because of this, locate searches much faster than the find command because it searches directly through the database, which is naturally faster.

The locate command displays the absolute path to the file when it finds it, such as:

But the locate command has a downside: it can’t search for files created that day because its database is updated only once a day, in the morning. For example, if I create a new file, locate can’t find it:

To solve this problem, we can manually update its database using the updatedb command:

$ sudo updadbCopy the code

Then, we can search for the new file.

3. which

The which command is used to find the location of the executable file. The location is specified in the $PATH and $MANPATH environment variables. By default, the which command displays the first location of the executable file:

If an executable file is stored in more than one location, you can use the -a option to list all locations.

If you want to find multiple files at once, you can simply follow the which command.

4. whereis

The whereis command looks for binaries, source code, and documents that contain a given query keyword in the system’s default installation directory (typically the software installed by default if you have root permission). (The default directories include /bin, /sbin, /usr/bin, /usr/lib, /usr/local/man, etc.).

It generally contains the following three parts:

  • Path to the binary file
  • Binary file source path
  • Path of the man file

For example, we now search for the ls command:

We can use the -b option to search only for the executable file location, the -b option to specify the search location, and the -f option to list the file information.

Similarly, we can use -s to restrict the search to the source path only, -m to search the man page path, -s to specify the search path to source files, and -m to specify the search path to help files.


Finally, recently, many friends asked me for Linux learning roadmap, so I stayed up for a month in my spare time according to my own experience, and sorted out an e-book. Whether you are interviewing or self-improvement, I believe will help you! The directory is as follows:

Free to everyone, just ask everyone to point to me!

Ebook | Linux development learning roadmap

Also hope to have a small partner can join me, do this e-book more perfect!

Have a harvest? Hope the old iron people come to a triple whammy, give more people to see this article

Recommended reading:

  • Dry goods | programmers advanced architect necessary resources free of charge
  • Artifact | support resource site search