How to Use All Linux’s Search Commands

Concept of a Linux terminal full of text on a laptop

Linux offers six different search methods, each with its advantages. We will show how to use find, local, what, whereis, whatis and apropos. Everyone excels in different tasks; Here's how to choose the right tool for the job.

You only have to be spoiled for choice when it comes to Linux search and search commands. Why so much? Well, they each have their specialties and work better than others under certain circumstances. You could think of them as a kind of Swiss army knife looking. We will examine each blade in turn and discover its strengths.

The find command

The behavior of the find command is difficult to determine by trial and error. Once you understand the syntaxyou begin to appreciate its flexibility and power.

The easiest way to use find is to just type find and press enter.

find

find command in a terminal window

Used in this way, find behaves like ls, but it lists all the files in the current directory and those in the subdirectories.

output of the find command in a terminal window

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

find .

find . command in a terminal window

For the search to be performed from the root folder, you would use this command:

find /

end of command in a terminal window

To start the search from your personal folder, use this command:

find ~

find ~ in a terminal window

Use find with file templates

In order for that find to be something more than a self-replicating version of ls, we must provide it with something to look for. We can provide file names or file templates. Templates use wildcards where * means any string and? means any single character.

Patterns must be shown to work properly. It's easy to forget to do it, but if you do not quote the generic pattern, you will not be able to execute the command you gave it correctly.

With this command, we will search in the current folder for the files corresponding to the template "*. * S ". This means any file name that has a file extension that ends with "s". We use the -name option to indicate that we are passing a file name or file name template.

find . -names"

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

find returns these matching files.

Note that two of the file extensions have two characters and the other three. That's because we used the pattern "*. * S ". If we only wanted two-character file extensions, we would have used "*.? S ".

outputting the file command in a terminal window

If we had known in advance that we were looking for JavaScript files ".js", we could have been more precise in our file model. Also note that you can use single quotes to wrap the pattern if you prefer.

find . -name & # 39; * .js & # 39;

find . -name "* .js" in a terminal window

This time, only find reports on JavaScript files.

JavaScript files found by find in a terminal window

Ignore the case with find

If you know the name of the file you want to look for, you can pass it on instead of a pattern. You do not need to enclose the file name in quotation marks unless it contains any wildcard characters, but it is recommended to do so all the time. This means that you will not forget to use them when you need them.

find . -name & # 39; Yelp.js & # 39;

find . -name & # 39; Yelp.js & # 39; in a terminal window

It did not return anything. But curious, we know that this file must be there. Let's try again and say to find to ignore the case. We do this using the -iname option (ignore the case name)

find. -iname & # 39; Yelp.js & # 39;

find. -iname & # 39; Yelp.js & # 39; in a terminal window

That was the problem, the file name starts with a tiny "y", and we were looking for a capital "Y".

Recurring subdirectories with find

An interesting aspect of finding is the way it performs recursive searches in subdirectories. Let's search for all the files starting with "map".

find . -name "map *. *"

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

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

Results of a subdirectory in a terminal window

Directory search with find

The -path option allows searching directories. Let's look for a directory that we do not remember the name anymore, but we know it ends with the letters "about".

find . -path & # 39; * about & # 39;

find . -path & # 39; * about & # 39; in a terminal window

The directory is found, it just calls "about" and it is nested in another directory of the current directory.

Directory found in a terminal window

There is a -ipath option (skip the case path) that allows you to search for paths and ignore case, similar to the -iname option described above.

Using file attributes with find

find can search for files whose attributes match the search index. For example, you can search for empty files using the -empty option, regardless of their name.

find . -empty

find . -empty in a terminal window

All zero-byte length files will be listed in the search results.

The -executable option will find any file that can be run, such as a program or script.

find . -exécutable

find . -executable in a terminal window

The results list a file called "fix_aptget.sh".

They also contain three directories, including ".", The current directory. The directories are included in the results because the execution bit is set in their file permissions. Without this, you would not be able to access these directories ("run").

Results of executable file search in a terminal window

The -type option

The -type option allows you to search for the type of object you are looking for. We will provide the "f" flag as a parameter of the -type option, because we want find to only search for files.

find . executable type f

find . executable -type f in a terminal window

This time the subdirectories are not listed. The executable script file is the only element of the results.

directory-less search results in a terminal window

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

find . type -d

find . type -d in a terminal window

Only directories and subdirectories are listed in the results.

directories listed in a terminal window

Use other commands with find

You can perform additional actions on the found files. You can also move the files to another command.

If we have to make sure that there is no executable file in the current directory and subdirectories, we could use the following command:

find . -name "fix_aptget.sh" -exec chmod -x # {} & # 39; ;

find . -name "fix_aptget.sh" -exec chmod -x # {} & # 39; ; in a terminal window

The command means:

Look in the current directory for an object called "fix_aptget.sh".
If it is found, run the chmod command.
The parameters passed to chmod are -x to remove executable permissions and & # 39; {} & # 39; which represents the file name of the found file.
The last semicolon marks the end of the parameters to be passed to chmod. This must be "escaped" by preceding it with a "" backslash.

Once this command is executed, we can search the executable files as before and this time no files will be listed.

Search results without an executable file in a terminal window

To expand our network, we could use a file template instead of the file name we used in our example.

This flexibility allows you to search for specified file types, or with file name templates, and perform certain actions on the corresponding files.

Find has many other options, including file search by modification date, files belonging to a user or group, readable files, or files with a specific set of file permissions.

The commands locate and locate

Previously, many Linux distributions included a copy of Locate. This was replaced by the mlocate command, which was an improved and updated version of locate.

When mlocate is installed on a system, it changes the locate command so that you actually use mlocate even if you type localization.

The current versions of Ubuntu, Fedora and Manjaro have been checked to see if any versions of these commands were already installed. Ubuntu and Fedora both included mlocate. It had to be installed on Manjaro, with this command:

sudo pacman -Syu mlocate
sudo pacman -Syu mlocate in a terminal window

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

If you use the –version with locate option, you will see that the responding command is actually mlocate.

locate –version

locate --version in a terminal window

As localize works on all Linux distributions tested, we will use locate in our explanations below. And it's one less letter to type.

The location database

The biggest advantage of localization is speed.

When you use the find command, it fires and searches your file system. Location control works very differently. It searches the database to determine if what you are looking for is on your computer. This makes the search much faster.

Of course, this raises an obvious question about the database. What guarantees that the database is up to date? When mlocate is installed, it places (usually) 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 can not find any entries, you can configure an automated task to do it for you at the time you choose.

RELATED: How to schedule tasks in Linux: Introduction to Crontab files

What happens if your computer is not turned on when the database needs to be updated? You can manually run the database update process by using the following command:

sudo updatedb

sudo updatedb in a terminal window

Using Locate

Let's search for files containing the string "getlatlong". With locate, the search automatically searches for matches containing the search term in the file name. It is therefore not necessary to use wildcards.

locate getlatlong
locate getlatlong in a terminal window

It is difficult to transmit speed in a screenshot, but the corresponding files are almost immediately listed for us.

locate results with files containing getlatlong in a terminal window

Tell locating how many results you want

Sometimes you know that there are many files of the type you are looking for. You only need to see the first ones. You may just want to know which directory they are in and you do not need to see all the filenames.

By using the -n (number) option, you can limit the number of results that will be located. In this command, we have defined a limit of 10 results.

locate .html -n 10

locate .html -n 10 in a terminal window

search for answers by listing the top 10 matching file names extracted from the database.

search results located in a terminal window, limited to 10 results

Count the corresponding files

If you only want to know the number of matching files and do not need to know how they are calling or where they are on your hard disk, use the -c (count) option. .

locate -c .html

locate -c .html in a terminal window

We know that there are 431 files with the extension ".html" on this computer. Maybe we want to look at them, but we thought to take a look and see how many there were first. With this knowledge, we know that we will have to carry less data.

locate .html | less

locate .html | less in a terminal window

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

html file listing channeled through less in a terminal window

Ignore the case with locate

This is exactly what the renter does -I (ignore case), he ignores the uppercase and lowercase differences between the search term and the file names in the database. If we try again to count the HTML files, but we mistakenly supply the search term in uppercase, we will not get any results.

locate -c .HTML

locate -c .HTML in a terminal window

By including the -i option, we can make localizes ignore the difference and return the expected answer for this machine, which is 431.

locate -c -i .HTML

locate -c -i .HTML in a terminal window

The status of the location database

To see the status of the database, use the -s (status) option. This causes locating to return statistics about the size and content of the database.

locate -s

locate -s in a terminal window

The command that

The command that searches in the directories of your way, and try to locate the command you are looking for. It allows you to determine the version of a program or command to execute when you type 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. He must be somewhere in the way because when we type his name, he runs it. We can use which to locate it with this command:

which geoloc

what geoloc in a terminal window

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

geoloc in / usr / local / bin

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

who -a geoloc

who -a geoloc in a terminal window

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

who -a geoloc in a terminal window

Of course, the copy in / usr / local / bin will be found first by the Bash shell each time, so having the program in two places does not make sense.

Removing the version in / usr / bin / geoloc will save you a bit of hard drive capacity. More importantly, it will also avoid the problems created by manually updating the program by someone who does it in the wrong place. Then you wonder why they do not see the new updates when they run the program.

The whereis command

The whereis command is similar to the which command, but it is more informative.

In addition to the location of the command file or program, whereis also indicates where the man (manual) pages and the source code the 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, the manual pages and the source code are often called "package" for this command. If you want to know where the different components of the diff package are, use the following command:

where is diff

where is diff in a terminal window

Whereis responds by indicating the location of the diff manual pages and the diff binary file.

where is result for diff in a terminal window

To restrict the results to only show the location of the binary file (in fact, use whereis as follows), use the -b (binary) option.

where is -b diff

where is -b diff in a terminal window

whereis only reports the location of the executable file.

where the output is limited to the bit location only in a terminal window

To limit the search to reports on man pages only, use the -m (manual) option. To limit the search to reports on source code files only, use the -s (source) option.

To see the locations where searches are done, use the -l (locations) option.

where is the

whereis -l in a terminal window

The locations are listed for you.

where search locations are listed in a terminal window

Now that we know the places we will search, we can, if we choose, limit the search to a particular place or group of places.

The -B (binary list) option limits the search for executable files to the list of paths provided on the command line. You must specify at least one location to search. The -f (file) option is used to signal the end of the last location of the beginning of the file name.

where is -B / bin / -f chmod

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

Whereis is the only place we asked to search. This is where the file is located.

whereis results from the use of the -B option in a terminal window

You can also use the -M (manual list) option to restrict manual page searches to paths that you specify on the command line. The -S option (source list) allows you to restrict the search for source code files in the same way.

Whatis command

The whatis command is used to quickly search the man pages. He offers summary descriptions of a line of the term you asked him to look for.

Let's start with a simple example. Although this seems to be the starting point for a deep philosophical debate, we are simply wondering what tells us what the term "man" means.

what is the man

whatis man in a terminal window

whatis finds two corresponding descriptions. He prints a short description for each match. It also lists the numbered section of the manual containing each complete description.

What happens in a terminal window

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

man 1 man

man 1 man in a terminal window

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

man page open to the first section in a terminal window

To open the manual in Section 7, on the page that describes the macros that you can use to generate manual pages, use this command:

man 7 man

man 7 man in a terminal window

The man macros man page is displayed.

man page opened in section seven in a terminal window

Search in specific sections of the manual

The option -s (section) limits the search to the sections of the manual that interest you. To limit whatis search to section 7 of the manual, use the following command. Note the quotation marks around the section number:

whatis-s "7" man

whatis -s "7" man in a terminal window

The results refer only to section 7 of the manual.

What are the results limited to section seven in a terminal window

Use whatis with wildcards

You can use wildcards with whatis. You must use the -w (wildcard) option to do this.

whatis -w char *

whatis -w char * in a terminal window

The corresponding results are listed in the terminal window.

whatis wildcard matches in a terminal window

The command apropos

The command apropos is similar to whatis, but it has still some bells and whistles. It performs a search in manual page titles and descriptions of a line in search of the search term. It lists the corresponding manual page descriptions in the terminal window.

The word apropos means "bound to" or "concerning", and the command apropos takes its name from that. To find any element related to the groups command, we can use this command:

groups about

groups in a terminal window

apropos displays the results in the terminal window.

results about a group in a terminal window

Using multiple search terms

You can use several search terms on the command line. apropos will search for manual pages that contain one of the searched terms.

about chmod chown

about chown chmod in a terminal window

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

apropos results for chmod and chown in a terminal window.

Using exact matches

apropos will return the manual pages containing the search term, even if that term is in the middle of another word. For apropos to return only exact matches for the search term, use the -e (exact) option.

To illustrate this, we will use apropos with grep as the search term.

about grep

about grep in a terminal window

Many results are returned for this, including many cases where grep is embedded in another word, such as bzfgrep.

results for apropos grep in a terminal window

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

about -e grep

about -e grep in a terminal window

We have only one result this time, for what we were really looking for.

results for apropos -e grep in a terminal window

Corresponding to all search terms

As we have seen before, if you provide multiple search terms, you will search for manual pages that contain either term. We can change this behavior by using the -a (and) option. Thus, apropos selects only the matches containing all the search times.

Let's try the command without the -a option to see what results give.

apropos crontab cron

a cron crontab cron in a terminal window

The results include manual pages corresponding to one or the other of the search terms.

results about crontab cron in a terminal window

We will now use the -a option.

apropos -a crontab cron

about -a crontab cron in a terminal window

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

result for apropos -a crontab cron n a terminal window

Even more options

All of these commands have more options – some of them much more – and we invite you to read the manual pages of the commands described in this article.

Here is a quick summary for each order:

find: provides a granular and detailed search feature for searching files and directories.
Locate: provides a database-based quick search for programs and commands
which: search in $ PATH for executable files
whereis: searches $ PATH for executable files, man pages, and source code files.
whatis: look for man-line descriptions of matches with the search term.
apropos: search the manual page with more fidelity than whatis, to search for matches with the search term (s).

Looking for more information about Linux terminals? here is 37 commands to know.

RELATED: 37 Important Linux commands to know

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.