قالب وردپرس درنا توس
Home / Tips and Tricks / How to Use All Linux Search Commands

How to Use All Linux Search Commands



  Concept of a Linux terminal full of text on a laptop

Linux offers six different search methods, each of which has its own merits. We will demonstrate how to use to locate locate where is what is and apropos . Everyone is characterized by different tasks; Here's how to choose the right tool for the job.

You're spoiled for choice when it comes to finding and finding commands in Linux. Why so many? Well, they each have their specialties and in some circumstances perform better than the others. You could think of it as a kind of Swiss army knife for the search. We will look at each blade in turn and find out their particular strengths.

The command find

The behavior of the command find is difficult to determine by trial and error. Once you understand the syntax, start to appreciate its flexibility and performance.

The simplest way to use find is to enter only find and press Enter

 . 1
9659008] find command in a terminal window " width="646" height="57" src="/pagespeed_static/1.JiBnMqyl6S.gif" onload="pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);" onerror="this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);"/>

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

 Issue of the Find Command in a Terminal Window

In some implementations of find you must put . for the current directory. If this is the case with your Linux version, use the following command:

  find. 

  find. Command in a Terminal Window

To locate in the root folder, use the following command:

  find / 

  fin d / command in a terminal window

Use this command to start the search from your home folder:

  find ~ 

  find ~ in a terminal window

Using find with file patterns

For find to Be something more than one automatically recursive version of ls we have to provide them with something we can search for. We can provide filenames or file patterns. Patterns use wildcards in which * any string and ? means any single character.

Patterns must be quoted to work correctly. It's easy to forget to do this, but if you do not specify the wildcard pattern find can not properly execute the command you gave it.

Search the current folder for files that match the pattern "*. * s "correspond. This means that each file name has a file extension ending with "s". We use the option -name to tell find that we are passing either a filename or a filename pattern.

  find. name "*. * s" 

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

find returns these matching files.

Note that two of the file extensions are two characters and one is three characters long. This is because we used the pattern "*. * S". If we just wanted the two-character file extensions, we would have used "*.? S".

 Output of the File Command in a Terminal Window

If we had known in advance, we searched for JavaScript JS files that we could have specified more accurately in our file pattern. Also note that you can use single quotes to break the pattern if you prefer.

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

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

This time finds only JavaScript files.

<img class = "alignnone size-full wp-image-425771" data-pagespeed-lazy-src = "https://www.howtogeek.com/wp-content/uploads/2019/06/xfind_9.png .pagespeed.gp + jp + jw + pj + ws + js + rj + rp + rw + ri + cp + md.ic.coChTWoonH.png "alt =" JavaScript files found by find in a terminal window [19659034] Ignoring case with find

If you know the name of the file you want to find you can pass it to find instead of a pattern Quote quotes if no wildcards are included, but it's always recommended to do so, which means you do not forget to use them when you need them.

  find. -Name & # 39; yelp.js & # 39; 

  find -name # yelp.js # in a terminal window

That did not return anything, but strangely, we know the file must be there. Let's try it einm al and say find that capitalization should be ignored. We do this with the option -iname (ignore case names)

 . -iname # Yelp.js & # 39; 

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

That was the problem, the file name starts with a lowercase letter "y" and we searched with a capital letter "Y".

Recurring subdirectories with find

A great find at find is the recursive search in subdirectories. Let's search for files that start with "map".

  find. name "map *. *" 

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

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

 Results from a subdirectory in a terminal window

Search for directories with find

The option -path causes find Find directories. Let's look for a directory whose name we can not remember exactly, but which ends with the letters "about".

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

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

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

 Directory found in A terminal window

There is an option -ipath (Ignore case) that searches for paths and is case-sensitive iname

Using file attributes with find

find can search for files whose attributes match the search hint. For example, you can use the -empty option to search for files that are empty, regardless of their names.

  find. -leer 

 . -in a terminal window blank

All zero-byte files are listed in the search results.

The executable option finds any file that can be executed, such as a program or a script.

  find. feasible 

 . -performable in a terminal window

The results contain a file named "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 option you could not switch to these directories ("execute").

 Search results for executables in the terminal window

The -type option

The You can use the -type option to search for the desired object type. We will specify the type indicator "f" as the parameter for the option type because find should only search for files.

  find. executable file of type f 

  find. executable file - type f in a terminal window

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

 Search results without directories in a terminal window

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

  find. Find type -d 

 . Type -d in a terminal window.

Only directories and subdirectories are listed in the results.

 Directories Listed in a Terminal Window

Using Other Commands with find

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

To make sure there are no executables in the current directory and subdirectories, you can 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:

  • Search the current directory for a named object named "fix_aptget.sh".
  • If found, execute the command chmod .
  • The parameters passed to chmod are -x to remove executable permissions, and & # 39; {} & # 39; representing the filename of the found file.
  • The last semicolon marks the end of the parameters passed to chmod . This must be preceded by a backslash.

After running this command, we can search for executables as before. This time, no files are listed.

 Search results without executable files in a terminal window

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

This flexibility allows you to search for specific file types or filename patterns and perform an action on the matching files.

Search provides many other options, including searching for files by modification date, a user's or group's files, files that are readable, or files with specific file permissions.

The locate and mlocate Commands

Many Linux distributions contained a copy of locate . This was superseded by the command mlocate which was an improved and updated version of locate .

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

Recent versions of Ubuntu, Fedora and Manjaro have been reviewed to see if they existed. Versions of these commands are preinstalled on them. Ubuntu and Fedora each included mlocate. It had to be installed on Manjaro with the following command:

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

On Ubuntu you can use locate and mlocate . interchangeable. On Fedora and Manjaro, you must enter locate but the command will be executed for you by mlocate .

If you locate the option - version with You will see that the command answering is actually mlocate .

  locate --version 

  locate --version in a terminal window

Because locate works on all tested Linux distributions. In the following explanations, locate is used. And there is only one letter left to enter.

Locate the database

The biggest advantage of locate is the speed.

When you use the find command, the dash appears and performs a search on your file system. The command locate works very differently. A database search is performed to see if what you are looking for is on your computer. This speeds up the search considerably.

Of course, it raises an obvious question about the database. What makes sure the database is up to date? If mlocate is installed, an entry is inserted in cron.daily . This is done every day (very early in the morning) and updates the database.

Use this command to check if this entry exists:

  ls /etc/cron.daily/*loc*[19659109[ls/etc/crondaily/*loc*inaterminalwindow" width="646" height="97" src="/pagespeed_static/1.JiBnMqyl6S.gif" onload="pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);" onerror="this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);"/> [19659005] If you can not find an entry there, you can set up an automated task to do it for you at the right time. 

RELATED: Scheduling Tasks on Linux: An Introduction to Crontab Files

What happens if your computer is not turned on at the time the database is refreshed shall be? You can manually run the database upgrade process with the following command:

  sudo updatedb 

  sudo updatedb in a terminal window

Using locate

Looking for files that contain the string "getlatlong". With locate, the search automatically searches for matches that contain the search term somewhere in the file name, so no wildcards need to be used.

  locate getlatlong 
  locate getlatlong in a terminal window 

It's hard to convey speed in a screenshot, but almost immediately the matching files are listed for us.

 Find results with files containing getlatlong in a terminal window.

Find how many results you want There are many files of the type you are looking for. You just have to see the first few of them. You may just want to be reminded of which directory you are in, and you do not need to see all the file names.

With the option -n (number) you can limit the number of results that locate will return to you. In this command, we have set a limit of 10 results.

  locate .html -n 10 

  locate .html -n 10 in a terminal window

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

 Search results limited to 10 results in a terminal window

Counting matching files

If you only want to know To determine the number of matching files and not knowing what they are called or where If they are on your hard drive, use the -c (count) option.

  locate -c .html 

  locate -c. HTML in a terminal window

Now we know that there are 431 files with the extension ".html" on this computer. We may want to look at them, but we thought we would take a look and see how many were there first. With this knowledge, we must direct the issue through less .

  locate .html | less 

  locate .html | less in a terminal window

And here they are all or at least high on the long list.

 HTML file list is passed through less in a terminal window.

Ignoring upper and lower case with locate

The -i (case insensitive) causes locate to do just that. In doing so, differences between the search term and the filename in the database are ignored. If we try to re-count the HTML files but inadvertently type the search term in uppercase, we will not get any results.

  locate -c .HTML 

  locate -c .HTML in a Terminal Window [19659003] By including the option -i we can cause to ignore the difference, and locate our expected answer for this machine, viz. 431.

  locate -c -i .HTML 

  locate -c -i .HTML in a terminal window

Locate the database state

Use the command -s (status) to display the status of the database. Possibility. As a result, locate returns some statistics about the size and content of the database.

  locate -s 

  locate -s in a terminal window

The which command

The command which searches the directories in your path and tries to find the command you are looking for Find. Here you can specify which version of a program or command is executed when you type its name in the command line.

Imagine we had a program called geoloc . We know it's installed on the computer, but we do not know where it is. It must be somewhere in the path, since it will be executed when we enter its name. With which we can find it with this command:

  which geoloc 

  which geoloc in a terminal window

The reports 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 in other locations within the path with the option -a ( Alles).

  which -a geoloc 

  which -a geoloc in a terminal window

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

 The -a geoloc in a terminal window

Of course, the copy is in / usr / local / bin Every time it is found first by the bash shell, so it is meaningless to have the program in two places.

Removing the version in / usr / bin / geoloc will save you a bit of disk space. More importantly, it avoids problems caused by a person manually updating and executing the program in the wrong place. Then ask yourself why the new updates do not appear when you run the program.

The command whereis

The command whereis is similar to the command which is more informative.

In addition to the location of the command or program file, whereis also indicates where the manual pages and source code files reside. In most cases, the source code files are not on your computer. If this is the case, will be referred to.

The executable binary, manpages, and source code are often referred to as "packages" for this command. If you want to know where the various components of the package are for the diff command, use the following command:

  whereis diff 

  whereis diff in a terminal window

whereis replies with a listing of the location of diff man pages and the diff binary.

 whereis appears in a terminal window for diff

To narrow the results so that only the location of the binary file is displayed (in fact: whereis works like which ), use the characters -b (binary) option.

  whereis -b diff 

  whereis -b diff in a terminal window

whereis specifies only the location of the executable file.

 where the output is restricted to the binary range in only one terminal window

To limit the search to report only on the man pages, use the -m ( manual) option. Use the option -s (source) to restrict the search so that only the source code files are reported.

To view the locations that searches, use the option. Option -l (Locations).

  whereis -l 

  whereis -l in a terminal window

The locations are listed for you.

 whereis search locations listed in a terminal window

Now that we know where is searched we can search on a specific location or group of locations restrict.

-B (binary list) restricts the search for executable files to the list of paths in the command line. You must specify at least one location where should be searched . The option -f (file) is used to indicate the end of the position before the file name starts.

  whereis -B / bin / -f chmod 

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

looks at the single place we wanted to search , In this case, the file is located.

 This results in the use of option -B in a terminal window.

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

The whatis command

Use the whatis command to restrict the search for source code files. Quickly search the man pages. It contains a one-line summary description of the term you were looking for.

Let's start with a simple example. Although it looks like the starting point of a deep philosophical debate, we ask just to say what the term "human" means.

  whatis man 

  whatis one finds in a terminal window

whatis finds two matching descriptions. There is a short description for each match. It also lists the numbered section of the manual that contains the full description.

   The result is a terminal window. 

Use man to open the manual The following command:

  Man 1 man 

  Man 1 man in a terminal window

The manual is located in the man ( 1) on the page for Man . [19659003]   Man page opened in section 1 in a terminal window

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

  man 7 man 

  man 7 man in a terminal window

The man page for the man macros will be displayed for you.

 Man page opened in section seven in a terminal window

Search in certain sections of the manual

The option -s (section) is used to search on sections Use the following command to restrict the search whatis to section 7 of the manual. 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.

 whatis results confined to section seven in a terminal window

Using whatis with wildcards

You can use wildcards with whatis . You must use the option -w (placeholder).

  whatis -w char * 

  whatis -w char * in a terminal window

The matching results are listed in the terminal window.

 whatis placeholder in a terminal window

The command apropos

The command apropos resembles whatis but it still has a few bells and whistles. It searches the manpage titles and one-line descriptions for the search term. It lists the corresponding manpage descriptions in the terminal window.

The word apropos means "related to" or "concerned", and the command apropos has its name derived from this. Use this command to search for items related to the groups command:

  apropos groups 

  apropos groups in a terminal window

apropos lists die Results in the terminal window.

 Using multiple search terms </h2>
<p>  You can use multiple search terms on the command line. <code> apropos </code> searches for man pages containing either <em> or </em> of search terms. </p>
<pre>  apropos chown chmod </pre>
<p><img class=

The results are listed as before. In this case, there is an entry for each search term.

 apropos yields chmod and chown in a terminal window.

Using Exact Matches

apropos wants to return manpages containing the search term, even if the term is in the middle of another word. Use -e (exactly) to cause that Propos returns only exact matches for the search term.

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

  apropos grep 

  apropos grep in a terminal window

Many results have been returned for it, including many in which grep is in another word like bzfgrep contain.

 Results for apropos grep in a terminal window

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

  apropos -e grep 

  apropos -e grep in a terminal window

This time we have a single result for what we were looking for. [19659003]   Results for apropos -e grep in a terminal window

Matching Keywords

As mentioned earlier, when you enter more than one search term apropos searches for man pages that are either or Search term included. We can change this behavior with the option -a (and). As a result, apropos selects only matches that contain all search times.

Let's try the command without the option -a to see what results are. apropos yields.

  apropos crontab cron 

  apropos crontab cron in a terminal window

The results contain man pages that match one or the other search term.

 apropos Results for crontab cron in a terminal window

Now we use the option -a .

  apropos -a crontab cron 

  apropos -a crontab cron in a terminal window

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

 Ergebnisse für apropos -a crontab cron na terminal window

Noch mehr Optionen

Alle Von diesen Befehlen haben einige mehr Optionen, und Sie werden aufgefordert, die Manpages für das c zu lesen Befehle, die wir in diesem Artikel erörtert haben.

Hier ist eine kurze Zusammenfassung für jeden Befehl:

  • find : Bietet eine funktionsreiche und differenzierte Suchfunktion zum Suchen nach Dateien und Verzeichnissen.
  • locate : Ermöglicht eine schnelle datenbankgesteuerte Suche nach Programmen und Befehlen.
  • which : Durchsucht den $ PATH nach ausführbaren Dateien
  • whereis : Sucht $ PATH sucht nach ausführbaren Dateien, Manpages und Quellcodedateien.
  • whatis : Durchsucht die einzeiligen Man-Beschreibungen nach Übereinstimmungen mit dem Suchbegriff.
  • apropos : Durchsucht die Manpage mit mehr Genauigkeit als whatis nach Übereinstimmungen mit dem Suchbegriff oder den Suchbegriffen.

Sie suchen nach mehr Informationen zum Linux-Terminal? Hier sind 37 Befehle, die Sie kennen sollten.

RELATED: 37 Wichtige Linux-Befehle, die Sie kennen sollten




Source link