Linux offers a few different ways to search, and each has its merits. We are going to teach you how to use them so that you can make more precise searches. These commands excel at different tasks and here’s how to choose the right tool for the job.
You have a lot of options when it comes to search and find commands in Linux. Why? Well, each one has their specialties and they perform better than the others in certain circumstances. You could think of them as a kind of Swiss army knife for searching. Let’s look at each blade 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.Some find implementations require you to put the “.” for the current directory. If this is the case with your version of Linux, use the following command:
Using find with file patterns in Linux
In order for the find command to be anything more than a version of ls automatic recursion, we must give it something to search for. We can provide file names or file patterns. The patterns make use of wildcards where * means any string of characters and ? means any single character.
You should note that two of the file extensions have two characters and one has three characters. This is because we use the pattern “*. * S’. If we only wanted the two-character file extensions, we would have used “*.? S’.
find . -name '*.js'
2.- Locate and mlocate commands
Many Linux distributions used to have a locate copy included with them. This was superseded by the mlocate command, which was an improved and updated version of locate. When mlocate is installed on a system, it modifies the locate command so that it is used by mlocate even if you type locate.
Linux: located databases
The biggest advantage that locate has is speed. When you use the find command, it bypasses and searches your file system. Because of this, the locate command works very differently. It performs a database search to determine if what it is looking for is on your pc. That makes the search much faster.
Tell locate how many results you want
You should know that there are many files of the type you are looking for. You only need to see the first ones. Or maybe you just want to be reminded what directory they are in and don’t need to see all the filenames.
Using the -n (number) option you can limit the number of results that locate will return. In this command, we have set a limit of 10 results.
locate .html -n 10
As you’ll see, the locate command responds by listing the first 10 matching file names 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. Allows you to determine which version of a program or command will be executed when you type its name on the command line.We can check if there are other copies of the program in other locations within the path using the -a (all) option.
which -a geoloc
This shows us that we have the geoloc program in two places. Of course, the /usr/local/bin copy will be found first by the Bash shell each time, so having the program in two places doesn’t make sense.Deleting the /usr/bin/geolocte version will save some hard drive capacity. More importantly, it will also prevent problems created by someone manually updating the program and doing it in the wrong place.
4.- Where is 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 man 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 them.To restrict the results to only showing the location of the binary (in effect, make whereis work like which) use the -b (binary) option.
5.- Command whatis
This command is used to quickly search through man pages. It provides one-line summary descriptions of the term you’ve asked it to search for.
whatis finds two matching descriptions. Prints a short description for each result. It also lists the section of the manual that contains each complete description.To open the manual to the section that describes the man command, use the following command:
man 1 man
As you will see, the manual opens in the man (1) section, in the manual page.
6.- Command apropos
The apropos command is similar to whatis, but has a few more bells and whistles. Searches through the man page titles and one line descriptions looking for the search term. It also lists the matching man page descriptions in the terminal window.
Using more than one search term on Linux
You can use more than one search term on the command line. apropos will look for man pages that contain any of the search terms you are about to type or want to know about.
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, here are the most important ones.