Fatmawati Ahmad Zaenuri / Shutterstock.com

Linux offers six different search methods, each with its own merits. We’ll show you how to use find , locate , which , whereis , whatis and apropos . Each excels at different tasks; Here’s how to choose the right tool for the job.

You are spoiled for choice when it comes to Linux search and search commands. Why so much? Well, each of them has its own characteristics, and they work better than others in certain circumstances. You could think of them as something like a Swiss army knife for searching. We will take a look at each blade in turn and figure out its strengths.

Search command

Team Behavior find difficult to determine by trial and error. Once you understand the syntax, you will begin to appreciate its flexibility and power.

The easiest way to use find — just dial find and press Enter.

  находить 

find command in terminal window

used in this way find Behave like ls but it lists all the files in the current directory and those in subdirectories.

output of find command in terminal window

Some implementations find require you to put . for the current directory. If this is the case on your version of Linux, use the following command:

  находить . 

find .  command in terminal window

To find search from root folder, you should use this command:

  находить / 

fin d /command in terminal window

To start searching from your home folder, use this command:

  найти ~ 

find ~ in terminal window

Using find with file patterns

To find was something more than an automatically recursive version ls we need to provide it with something to look for. We can provide filenames or file patterns. Patterns use wildcards where * means any character string and ? means any single character.

Templates must be specified to work correctly. It’s easy to forget to do this, but if you don’t quote the pattern, find will not be able to correctly execute the command you gave it.

With this command, we will search the current folder for files that match the pattern «*. *S». This means that any filename has a file extension ending in «s». We use the option -name to let you know find we pass a filename or filename pattern.

  находить .  -имя "*. * s" 

find .  -name "*.*s" in terminal window

find returns those matching files.

Note that two file extensions are two characters long and one is three characters long. This is because we used the pattern «*. *S». If we only wanted two character file extensions, we would use «*.? S.

output of file command in terminal window

If we knew ahead of time that we were looking for JavaScript «.js» files, we could be more specific in our file template. Also note that you can use single quotes to wrap the pattern if you like.

  находить .  -имя '* .js' 

find .  name "*.js" in terminal window

This time find only reports on JavaScript files.

JavaScript files found by the find command in a terminal window

Ignoring the case with the find

If you know the name of the file you want find to find you can pass it to find instead of a template. You don’t need to enclose the filename in quotation marks unless it contains wildcards, but it’s recommended that you always do so. This means you won’t forget to use them when you need them.

  находить .  имя Yelp.js 

find .  name 'Yelp.js' in terminal window

It didn’t return anything. But strangely, we know the file must be there. Let’s try again and say find to ignore case. We do this with the option -iname (ignore case name)

  находить.  - имя Yelp.js 

find.  - the name of Yelp.js in the terminal window

This was the problem, the filename starts with a lowercase «y» and we were looking for an uppercase «Y».

Recursive subdirectories with find

Distinctive feature find is how it recursively looks through subdirectories. Let’s find any files that start with «map».

  находить .  -имя "карта *. *" 

find .  name "map *.*" in terminal window

The relevant files are listed. Note that they are all in a subdirectory.

Results from a subdirectory in a terminal window

Search for directories with a find

Option -path allows you to search directories. Let’s look at a directory whose name we can’t remember but we know ends with ‘o’s.

  находить .  -path '* about' 

find .  -path '* about' in a terminal window

The directory is found, it’s just called «about» and is nested in another directory in the current directory.

Found directory in terminal window

Exists -ipath (ignore path to -ipath ) which allows you to search for paths and ignore case, similar to the option — iname described above.

Using file attributes with find

find can search for files whose attributes match the search hint. For example, you can search for empty files using the option -empty regardless of what they are called.

  находить .  -empty 

find .  blank in terminal window

Any zero length files will be listed in the search results.

Option -executable will find any file that can be executed, such as a program or script.

  находить .  -executable 

find .  -executable in a terminal window

The list of results shows a file named «fix_aptget.sh».

They also contain three directories, including «.», the current directory. The directories are included in the results because the execute bit is set in their file permissions. Without this, you won’t be able to modify («run») these directories.

search results for an executable file in a terminal window

-type option

Option -type allows you to search for the type of object you are looking for. We are going to provide an indicator like «f» as an option parameter -type because we want the search to only search for files.

  находить .  исполняемый тип -f 

find .  -type f executable in terminal window

This time the subdirectories are not listed. The script executable is the only item in the results.

search results without directories in terminal window

We can also ask find include only directories in the results. To list all directories we can use the option -type with indicator type «d».

  находить .  тип -d 

find .  type -d in terminal window

Directories only list directories and subdirectories.

directories listed in the terminal window

Using other commands with find

You can perform some additional actions on the found files. You can pass the files in turn to some other command.

If we need to make sure that there are no executable files in the current directory and subdirectories, we could use the following command:

  находить .  -name "fix_aptget.sh" -exec chmod -x '{}' \; 

find .  -name

Command means:

  • Look in the current directory for a named object named «fix_aptget.sh».
  • If found, run the command chmod .
  • Parameters passed to chmod : -x to remove the rights to the executable file and '{}' which represents the filename of the found file.
  • The last semicolon marks the end of the parameters to be passed to chmod . This needs to be «avoided» by putting a backslash in front of it.

After running this command, we can search for executable files as before, and this time there will be no files in the list.

Search results without executables in a terminal window

To extend our network, we could use a file pattern instead of the filename we used in our example.

This flexibility allows you to search for files of specific types or filename patterns, and perform some action on the corresponding files.

The search has many other options, including searching for files by the date they were modified, files owned by a user or group, files that are readable, or files with a specific set of file permissions.

locate and mlocate commands

Many Linux distributions have a copy locate . This has been replaced by the command mlocate which was an improved and updated version locate .

When mlocate installed on the system, it changes the command locate so you are actually using mlocate even if you enter locate .

Current versions of Ubuntu, Fedora and Manjaro have been checked for pre-installed versions of these commands. Ubuntu and Fedora included mlocate. It had to be installed on Manjaro with this command:

  Судо Пакман -Сю Мокате 
sudo pacman -su navigate in terminal window

On Ubuntu you can use locate and mlocate interchangeably. On Fedora and Manjaro, you must type locate but the command is executed for you mlocate .

If you use the option --version With locate you will see that mlocate actually mlocate .

  найти - версия 

locate --version in terminal window

Because the locate works on all tested Linux distributions, we will use locate in our explanation below. And that’s one letter less.

locate database

The biggest advantage locate is the speed.

When you use the command find it breaks and searches your file system. Team locate works quite differently. It searches the database to see if what you are looking for is on your computer. This makes searching much faster.

Of course, this raises the obvious question of the database. What keeps the database up to date? When mlocate is set, it (usually) puts an entry in cron.daily . This runs every day (very early in the morning) and updates the database.

To check if this entry exists, use this command:

  ls /etc/cron.daily/*loc* 

ls /etc/cron.daily/*loc* in a terminal window

If you don’t find an entry there, you can set up an automatic job to do it for you at any time.

RELATED: How to Schedule Tasks in Linux: An Introduction to Crontab Files

What if your computer is not turned on when the database is supposed to be updated? You can manually start the database upgrade process with the following command:

  sudo updatedb 

sudo updatedb in a terminal window

Using locate

Let’s look at the files that contain the string «getlatlong». With locate, the search automatically looks for any matches containing the search word anywhere in the file name, so there is no need to use wildcards.

  найти getlatlong 
search for getlatlong in a terminal window

It’s hard to capture the speed in a screenshot, but almost immediately the relevant files are listed for us.

find results with files containing getlatlong in a terminal window

Speak find how many results you want

Sometimes you may know that there are many files of the type you are looking for. You only need to see the first few of them. Maybe you just want to be reminded what directory they are in and you don’t need to see all the filenames.

Using the option -n (number), you can limit the number of results that will be returned to you. In this command, we set a limit of 10 results.

  найдите .html -n 10 

search for .html -n 10 in a terminal window

locate responds by listing the first 10 matching filenames it retrieves from the database.

search results from limited to 10 results in a terminal window

Matching files count

If you only want to know the number of matching files and don’t need to know what they are called or where they are on your hard drive, use the -c (count) option.

  найдите -c .html 

look for -c .html in a terminal window

So now we know that there are 431 «.html» files on this computer. Maybe we want to take a look at them, but we thought we’d take a look and see how many there were in the first place. Armed with this knowledge, we know that we will need to communicate results through less .

  найти .html |  Меньше 

find .html |  less in terminal window

And here they are, or at least the top of a long list of them.

html file list skips less in terminal window

Ignoring the case with locate

-i (ignore case) forces locate to do just that, it ignores uppercase and lowercase differences between the search term and filenames in the database. If we try to count the HTML files again, but mistakenly enter the search term in uppercase, we get zero results.

  найдите -c .HTML 

look for -c .html in a terminal window

By enabling the option -i we can force locate ignore the case difference and return the expected response for that machine, which is 431.

  найдите -c -i .HTML 

search for -c -i .HTML in a terminal window

Find the state of the database

To see the state of the database, use the option -s (status). It makes locate return some statistics about the size and contents of the database.

  найти -s 

look for -s in a terminal window

Team which

Team which looks for directories in your path and tries to find the command you are looking for. This allows you to determine which version programs or teams will be executed when you enter its name on the command line.

Imagine we have a program called geoloc . We know that it is installed on the computer, but we do not know where it is located. It must be somewhere along the way, because when we type in its name, it will run. We can use to find it with this command:

  какой геолок 

what geoloc in the terminal window

which indicates that the program is in /usr/local/bin .

geolocation to /usr/local/bin

We can check if there are other copies of the program in other places along the path using the option -a (all).

  который-геолокация 

which-geolocation in terminal window

This shows us that we have a program geoloc in two places.

which-geolocation in terminal window

Of course, a copy /usr/local/bin will always be detected first by the Bash shell, so it’s pointless to have a program in two places.

Removing a version in /usr/bin/geoloc save you some hard drive space. More importantly, it will also avoid problems created by someone manually updating the program and doing it in the wrong place. Then I wonder why they don’t see new updates when they run the program.

whereis command

Team whereis looks like a team which but it is more informative.

In addition to the location of the batch or program file, whereis it also indicates where the manual (manual) pages and source code files are located. In most cases, the source code files will not be on your computer, but if they are, they will be reported.

The binary executable, man pages, and source code are often referred to as the «package» for this command. If you want to know where the various package components are for a command diff use the following command:

  где разница 

where is the difference in terminal window

whereis answers by listing the location of the manual pages diff binary file diff .

where are the results for diff in terminal window

To limit the results to only displaying the location of the binary (essentially whereis work whereis similar), use the parameter -b (binary).

  whereis -b diff 

whereis -b diff in terminal window

whereis reports only the location of the executable file.

where output is limited to binary location only in terminal window

To limit your search to a report only in the man pages, use the option -m (management). To limit your search to reports on source code files only, use the option -s (source).

To see the locations being searched, use the option -l (locations).

  где-я 

where -l in terminal window

The places are listed for you.

where the search locations are listed in the terminal window

Now that we know the locations that will be searched, we can, if we want, limit the search to a specific location or group of locations.

Option -B (binary list) limits the search for executable files to the list of paths specified on the command line. You must specify at least one location to search. Option -f (file) is used to indicate the end of a location, the last beginning of a filename.

  где -B / bin / -f chmod 

where -B /bin / -f chmod in a terminal window

whereis looks at the only place we asked to find. It happens where the file is located.

where is the result of using the -B option in the terminal window

You can also use the option -M (manual list) to limit the search for man pages to paths specified on the command line. Option -S (list of sources) allows you to limit the search for source code files in the same way.

Team Whatis

Team whatis used for quick search through manual (manual) pages. It contains a one-line short description of the term you asked for.

Let’s start with a simple example. Although this looks like the starting point of a deep philosophical debate, we are simply asking what to tell us what the term «person» means.

  что за человек 

What is a person in a terminal window

whatis finds two matching descriptions. It prints a short description for each match. It also lists the numbered sections of the manual that contain each full description.

which brings up the terminal window

To open the manual in the section describing the command man use the following command:

  человек 1 человек 

person 1 person in the terminal window

The manual opens in the man(1) section of the page man .

The man page opens in the first section in a terminal window

To open the manual in section 7, on a page that discusses macros that can be used to create manual pages, use this command:

  человек 7 человек 

people 7 people in the terminal window

The man page for the man macro is displayed for you.

man page opened in the seventh section in a terminal window

Search in individual sections of the manual

Option -s (section) is used to limit the search to the sections of the manual you are interested in. To limit your search whatis section 7 of the manual, use the following command. Notice the quotes around the section number:

  whatis -s "7" человек 

whatis -s "7" person in terminal window

The results refer only to section 7 of the manual.

Whatis results are limited to section seven in a terminal window

Using whatis with wildcards

You can use wildcards with whatis . For this you have to use the option -w (wildcard).

  whatis -w char * 

whatis -w char * in a terminal window

The matching results are listed in the terminal window.

wildcard whatis matches terminal window

About the team

Team apropos similar to whatis but it has a few more bells and whistles. It scans the manual page titles and single line descriptions looking for a search term. It lists the relevant man page descriptions in the terminal window.

The word apropos means «associated with» or «regarding» and the command apropos got its name from this. To find anything related to a command groups we can use this command:

  по поводу группы 

about the group in the terminal window

apropos lists the results in a terminal window.

about the results for the group in the terminal window

Using more than one search term

You can use more than one search term on the command line. apropos will look for man pages containing any from the search terms.

  по поводу чоун chmod 

about chown chmod in terminal window

The results are listed as before. In this case, there is one entry for each of the search terms.

Regarding the results for chmod and chown in the terminal window.

Using Exact Matches

apropos will return man pages that contain the search term, even if that term is in the middle of another word. To make sure that only exact matches for a search query are returned, use the option -e (accurate)

To illustrate this, we will use apropos With grep as a search query.

  по поводу grep 

about grep in a terminal window

This returns many results, including many where grep included in another word such as bzfgrep .

grep results in terminal window

Let’s try it again and use the option -e (exact).

  по поводу -е grep 

about -e grep in a terminal window

This time we have one result that we were really looking for.

results about -e grep in a terminal window

Match all search terms

As we saw earlier, if you enter more than one search term, apropos will look for man pages that contain any search term. We can change this behavior using the option -a (and). This allows you to select only those matches that have the entire search time.

Let’s try the command without the option -a so we can see what results it gives apropos .

  по поводу crontab cron 

about crontab cron in terminal window

The results include help pages that match one or the other of the search terms.

about the results of crontab cron in a terminal window

Now we will use the option -a .

  по поводу-crontab cron 

btw -a crontab cron in terminal window

This time, the results are narrowed down to those containing both search terms.

results about -a crontab cron in terminal window

Even more options

All of these commands have more options — some of them have many more options — and you are encouraged to read the man pages for the commands we have discussed in this article.

Here is a brief overview of each command:

Looking for more information about the Linux terminal? Here are 37 commands you should know.

RELATED: 37 Important Linux Commands You Should Know

Похожие записи