Linux offers some different ways of searching, and each has its merits. We are going to show you how to use them so you can do more precise searches. These commands stand out in different tasks and here we show you how to choose the right tool for the job.
You have many options when it comes to commands to search and find in Linux. Why? Well, everyone has their specialties and they perform better than others in certain circumstances. You could think of them as a kind of Swiss army knife to look for. We will see each leaf in turn and discover its particular strengths.
1.- Find command
The behavior of the find command is difficult to determine by trial and error. Once you understand the syntax, you begin to appreciate its flexibility and power.
The easiest way to use find is to simply type "find" and press enter.
find
Used in this way, find behaves like ls, but lists all the files in the current directory and those in subdirectories.
Some find implementations require that you place the "." For the current directory. If this is the case with your version of Linux, use the following command:
find .
To have find from the root folder, use this command:
find /
To start the search from your home folder, use this command:
find ~
Using find with file patterns in Linux
To be the find command more than an automatic recursion version ls, we must provide something to search. We can provide file names or file patterns. The patterns make use of wildcards where * means any string of characters and? means any individual character.
The patterns must be cited to work properly. It is easy to forget to do this, but if you do not quote the wildcard pattern, you 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 any file name that has a file extension that ends in "s". We use the -name option to tell find that we are passing a file name or a file name pattern.
find . -name "*.*s"
You should keep in mind that two of the file extensions have two characters and one has three characters. This is because we use the pattern «*. * S ». If we had only wanted the two character file extensions, we would have used «*.? S ».
If we had known beforehand that we were looking for JavaScript “.js” files, we could have been more specific in our file pattern. Also, keep in mind that you can use single quotes to adjust the pattern if you prefer.
find . -name '*.js'
2.- Locate and mlocate commands
Many Linux distributions used to have a locate copy included with them. This was replaced by the mlocate command, which was an improved and updated version of locate. When mlocate is installed on a system, modify the locate command to use mlocate even if you type locate.
The current versions of Ubuntu, Fedora and Manjaro were checked to see if they had preinstalled versions of these commands. Ubuntu and Fedora have both commands included. So with this command you will install it in Manjaro:
sudo pacman -Syu mlocate
In Ubuntu, you can use locate and mlocate interchangeably. In Fedora and Manjaro you must type locate, but the command is executed by the system using mlocate. Now, if you use the –version with locate option, you will see that the command that responds is actually mlocate.
locate --version
Linux: locate database
The biggest advantage that locate has is speed. When you use the find command, it deviates and performs a search on your file system. Therefore, the locate command works very differently. Perform a search in the database to determine if what you are looking for is on your pc. That makes the search much faster.
Of course, it raises an obvious question about the database. What ensures that the database is up to date? When mlocate is installed, it usually places an entry in cron.daily. This runs every day (very early in the morning) and updates the database.
To verify if this entry exists, use this command:
ls /etc/cron.daily/*loc*
Tell locate how many results you want
You should know that there are many files of the type you are looking for. You just need to see the first ones. Or maybe you just want to be reminded of what directory they are in and you don't need to see all the file names.
Using the -n (number) option you can limit the amount of results that locate will return you. In this command, we have set a limit of 10 results.
locate .html -n 10
As you will see, the locate command responds by listing the first 10 matching file names that it retrieves from the database.
3.- Command which
The which command searches through the directories in your path and tries to locate the command you are looking for. It allows you to determine which version of a program or command will be executed when you type its name on the command line.
Imagine we have a program called «geoloc». We know it is installed on the computer, but we do not know where it is located. It must be somewhere along the route because when we write your name, it runs. We can use which to locate it with this command:
which geoloc
which informs that the program is in / usr / local / bin.
We can verify if there are other copies of the program in other locations within the route using the -a (all) option.
which -a geoloc
This shows us that we have the geoloc program in two places. Of course, the copy / usr / local / bin will be found first by the Bash shell every time, so having the program in two places makes no sense.
Deleting the version / usr / bin / geolocte will save some capacity on the hard drive. More importantly, it will also avoid problems created by someone who manually updates the program and does it in the wrong place.
4.- Whereis command
The whereis command is similar to the which command, but is more informative. In addition to the location of the command or program file, whereis also reports where the manual pages and source code files are located. In most cases, the source code files will not be on your PC, but if they are, whereis will report on them.
The binary executable, the man pages and the source code are often referred to as "package" for that command. If you want to know where the various components of the package for the diff command are located, you have to write the following in the terminal:
whereis diff
whereis responds by listing the location of the diff man pages and the diff binary file.
To narrow the results so that they only show the location of the binary (in effect, make whereis work like which) use the -b (binary) option.
5.- Whatis command
This command is used to quickly search the "man" pages (manual). Provide summary descriptions on a line of the term that you have asked to search.
whatis man
whatis finds two matching descriptions. Print a brief description for each result. It also lists the section of the manual that contains each complete description.
To open the manual in the section that describes the man command, use the following command:
man 1 man
As you will see, the manual opens in the man section (1), on the manual page.
6.- Appropriate command
The apropos command is similar to whatis, but it has some more bells and whistles. Search through the titles of the manual pages and the descriptions of a line searching for the search term. As well as listing the matching descriptions of the manual page in the terminal window.
The word apropos means "related to" or "concerning," so the apropos command took its name from this. To find anything related to the groups command, you can type this in the terminal:
apropos groups
As you can see, apropos lists the results in the terminal window.
Use more than one search term in Linux
You can use more than one search term on the command line. apropos will search manual pages that contain any of the search terms you are going to type or those you want to know.
apropos chown chmod
The results are listed as before. In this case, there is only one entry for each of the search terms.
You have more options in Linux!
All these commands have more options and we recommend that you read the manual pages of the commands that all the distros of this great operating system have. For now, you have the most important here.
Here is a quick summary for each command:
- find: provides a granular and feature-rich search function for searching files and directories.
- locate: provides a quick database-based search for programs and commands.
- which: Search $ PATH to find executable files.
- whereis: Search the $ PATH to find executable files, manual pages and source code files.
- whatis: searches the descriptions of a "man" line (manual) for matches to the search term.
- apropos: search the manual page with more fidelity than whatis, to find matches with the search term or terms.
The list of useful commands for Linux is endless, you just have to take out your internal researcher and discover the commands that are most useful to you.