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.
находить
used in this way find Behave like ls but it lists all the files in the current directory and those in subdirectories.
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:
находить .
To find search from root folder, you should use this command:
находить /
To start searching from your home folder, use this command:
найти ~
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 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.
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'
This time find only reports on JavaScript files.
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
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
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».
находить . -имя "карта *. *"
The relevant files are listed. Note that they are all in a subdirectory.
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'
The directory is found, it’s just called «about» and is nested in another directory in the current directory.
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
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
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.
-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
This time the subdirectories are not listed. The script executable is the only item in the results.
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
Directories only list directories and subdirectories.
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:
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.
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:
Судо Пакман -Сю Мокате
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 .
найти - версия
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*
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
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
It’s hard to capture the speed in a screenshot, but almost immediately the relevant files are listed for us.
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
locate responds by listing the first 10 matching filenames it retrieves from the database.
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
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 | Меньше
And here they are, or at least the top of a long list of them.
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
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
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
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:
какой геолок
which indicates that the program is in /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).
который-геолокация
This shows us that we have a program geoloc in two places.
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:
где разница
whereis answers by listing the location of the manual pages diff binary file diff .
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 reports only the location of the executable file.
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).
где-я
The places are listed for you.
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
whereis looks at the only place we asked to find. It happens where the file is located.
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.
что за человек
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.
To open the manual in the section describing the command man use the following command:
человек 1 человек
The manual opens in the man(1) section of the page man .
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 человек
The man page for the man macro is displayed for you.
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" человек
The results refer only to section 7 of the manual.
Using whatis with wildcards
You can use wildcards with whatis . For this you have to use the option -w (wildcard).
whatis -w char *
The matching results are listed in the 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:
по поводу группы
apropos lists the results in a 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
The results are listed as before. In this case, there is one entry for each of the search terms.
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
This returns many results, including many where grep included in another word such as bzfgrep .
Let’s try it again and use the option -e (exact).
по поводу -е grep
This time we have one result that we were really looking for.
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
The results include help pages that match one or the other of the search terms.
Now we will use the option -a .
по поводу-crontab cron
This time, the results are narrowed down to those containing both search terms.
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:
find : Provides rich and detailed search to find files and directories.
locate : Provides quick search for programs and commands based on the database.
which the : searches $PATH looking for executables
whereis : Looks in $PATH for executables, man pages, and source code files.
whatis : Performs search in single-line man descriptions to find a match for the search term.
apropos : Search for a manual page with more precision than whatis to match a search query or terms.
Looking for more information about the Linux terminal? Here are 37 commands you should know.
RELATED:37 Important Linux Commands You Should Know