linux 搜索命令_如何使用所有Linux的搜索命令

linux 搜索命令

linux 搜索命令

Concept of a Linux terminal full of text on a laptop
Fatmawati Achmad Zaenuri/Shutterstock.com Fatmawati Achmad Zaenuri / Shutterstock.com

Linux offers six different ways to search, and each has its merits. We’ll demonstrate 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.

Linux提供了六种不同的搜索方式,每种都有其优点。 我们将演示如何使用findlocatewhichwhereiswhatis ,和apropos 。 每个人都擅长不同的任务; 这是为工作选择正确工具的方法。

You’re spoiled for choice when it comes to commands for searching and finding in Linux. Why so many? Well, they each have their specialties and perform better than the others in certain circumstances. You could think of them as a sort of Swiss-Army knife for searching. We’re going to look at each blade in turn and find out its particular strengths.

在Linux中进行搜索和查找的命令时,您会无所适从。 为什么那么多? 好吧,他们每个人都有自己的专长,并且在某些情况下的表现要比其他人更好。 您可以将它们视为一种搜寻的瑞士军刀。 我们将依次研究每个刀片,并找出其特定的优势。

find命令 (The find Command)

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

很难通过反复试验来确定find命令的行为。 一旦了解了语法 ,便会开始欣赏它的灵活性和功能。

The simplest way to use find is to just type find and hit enter.

使用find的最简单方法是只键入find并按Enter。

find
find command in a terminal window

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

以这种方式使用的find行为类似于ls ,但是它列出了当前目录中的所有文件以及子目录中的所有文件。

output from 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某些实现要求您放置. 用于当前目录。 如果您Linux版本是这种情况,请使用以下命令:

find .
find . command in a terminal window

To have find search from the root folder you’d use this command:

要从根文件夹中find搜索,请使用以下命令:

find /
fin d/ command in a terminal window

To start the search from your home folder use this command:

要从主文件夹开始搜索,请使用以下命令:

find ~
find ~ in a terminal window

将find与文件模式一起使用 (Using find With File Patterns)

For find to be something more than an auto-recursing version of ls, we must provide it with something to search for. We can provide filenames or file patterns. Patterns make use of wildcards where * means any string of characters and ? means any single character.

为使find不仅仅是ls的自动递归版本,我们必须为其提供要搜索的内容。 我们可以提供文件名或文件模式。 模式使用通配符,其中*表示任何字符串,而? 表示任何单个字符。

Patterns must be quoted to work correctly. It is easy to forget to do this, but if you don’t quote the wildcard pattern find won’t be able to properly carry out the command you gave it.

模式必须加引号才能正常工作。 忘记这样做很容易,但是如果您不引用通配符模式, find将无法正确执行您给它的命令。

With this command, we’re going to search in the current folder for files that match the pattern “*.*s”. This means any filename that has a file extension that ends in “s”. We use the -name option to tell find we’re either passing in a filename or a filename pattern.

使用此命令,我们将在当前文件夹中搜索与模式“ *。* s”匹配的文件。 这意味着任何文件扩展名以“ s”结尾的文件名。 我们使用-name选项告诉find我们正在传递文件名或文件名模式。

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

find returns these matching files.

find返回这些匹配文件。

Note that two of the file extensions are two characters long and one is three characters long. This is because we used the pattern “*.*s”. If we’d only wanted the two character file extensions, we would have used “*.?s”.

请注意,两个文件扩展名的长度为两个字符,一个为三个字符。 这是因为我们使用了模式“ *。* s”。 如果我们只想要两个字符文件扩展名,我们将使用“ *。?s”。

output of the file command in a terminal window

If we’d known in advance that we were looking for JavaScript “.js” files we could have been more specific in our file pattern. Also, note that you can use single quote marks to wrap the pattern if you prefer.

如果我们事先知道我们在寻找JavaScript“ .js”文件,那么我们的文件模式可能更具体。 另外,请注意,您可以根据需要使用单引号将模式引起来。

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

This time find only reports on the JavaScript files.

这次仅find有关JavaScript文件的报告。

JavaScript files found by find in a terminal window

忽略带有查找的案例 (Ignoring Case With find)

If you know the name of the file you want find to locate, you can pass that to find instead of a pattern. You don’t need to wrap the filename in quotes if there are no wildcards in it, but it is good practice to do it all the time. Doing so means you won’t forget to use them when do you need them.

如果知道要find的文件的名称,则可以将其传递给find而不是模式。 如果文件名中没有通配符,则无需在文件名中使用引号将其引起来,但始终建议这样做。 这样做意味着您在需要它们时不会忘记使用它们。

find . -name 'Yelp.js'
find . -name 'Yelp.js' in a terminal window

That didn’t return anything. But’s odd, we know that file must be there. Let’s try again and tell find to ignore case. We do that by using the -iname option (ignore case name)

那什么也没返回。 但是奇怪的是,我们知道文件必须存在。 让我们再试一次,告诉find忽略大小写。 我们通过使用-iname选项(忽略大小写名称)来实现

find. -iname 'Yelp.js'
fin d. -iname 'Yelp.js' in a terminal window

That was the problem, the filename starts with a lowercase “y”, and we were searching with an uppercase “Y.”

就是这个问题,文件名以小写的“ y”开头,而我们以大写的“ Y”搜索。

用find递归子目录 (Recursing Subdirectories with find)

One great thing about find is the way it recursively searches through subdirectories. Let’s search for any files that start with “map.”

find一大优点是它以递归方式搜索子目录的方式。 让我们搜索以“ 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

使用find搜索目录 (Searching for Directories With find)

The -path option makes find look for directories. Let’s look for a directory that we can’t quite remember the name of, but we know it ends with the letters “about.”

-path选项使find查找目录。 让我们寻找一个我们不太记得其名称的目录,但是我们知道它以字母“ about”结尾。

find . -path '*about'
find . -path '*about' in a terminal window

The directory is found, it is just called “about,” and it is nested inside another directory within the current directory.

找到该目录,它仅称为“ about”,并且嵌套在当前目录中的另一个目录中。

Found directory in a terminal window

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

有一个-ipath (忽略大小写路径)选项,使您可以搜索路径并忽略大小写,类似于上面讨论的– iname选项。

将文件属性与find一起使用 (Using File Attributes with find)

find can look for files that have attributes that match the search clue. For example, you can look for files that are empty using the -empty option, regardless of what they’re called.

find可以查找具有与搜索线索匹配的属性的文件。 例如,无论使用什么名称,都可以使用-empty选项查找空文件。

find . -empty
find . -empty in a terminal window

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

任何零字节长度的文件都将在搜索结果中列出。

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

-executable选项将查找任何可以执行的文件,例如程序或脚本。

find . -executable
find . -executable in a terminal window

The results list a file called “fix_aptget.sh”.

结果列出了一个名为“ 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 wouldn’t be able to change into (“run”) those directories.

它们还包含三个目录,包括当前目录“。”。 目录包含在结果中,因为在文件许可权中设置了执行位。 没有这个,您将无法更改(运行)这些目录。

executable file search results na terminal window

-type选项 (The -type Option)

The -type option allows you to search for the type of object you are looking for. We’re going provide the type indicator “f” as a parameter to the -type option because we want find to search for files only.

-type选项允许您搜索要查找的对象的类型。 我们将提供类型指示器“ f”作为-type选项的参数,因为我们只想find文件。

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 item in the results.

这次没有列出子目录。 可执行脚本文件是结果中的唯一项目。

search results with no directories in a terminal window

We can also ask find to only include 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
find . type -d in a terminal window

Only directories and subdirectories are listed in the results.

结果中仅列出目录和子目录。

directories listed in a terminal window

将其他命令与find一起使用 (Using Other Commands With find)

You can perform some additional action on the files that are found. You can have the files passed, in turn, to some other command.

您可以对找到的文件执行一些其他操作。 您可以依次将文件传递给其他命令。

If we need to make sure there are no executable files in the current directory and subdirectories, we could use the following command:

如果需要确保当前目录和子目录中没有可执行文件,则可以使用以下命令:

find . -name "fix_aptget.sh" -exec chmod -x '{}' \;
find . -name "fix_aptget.sh" -exec chmod -x '{}' \; in a terminal window

The command means:

该命令的意思是:

  • Search in the current directory for a named object called “fix_aptget.sh”.

    在当前目录中搜索名为“ fix_aptget.sh”的命名对象。
  • If it is found execute the chmod command.

    如果找到,请执行chmod命令。

  • The parameters that are passed to chmod are -x to remove executable permissions and '{}' which represents the filename of the found file.

    传递给chmod的参数是-x用于删除可执行权限)和'{}' (代表找到的文件的文件名)。

  • The final semicolon marks the end of the parameters that are going to be passed to chmod. This has to be ‘escaped’ by preceding it with a ‘\’ backslash.

    最后的分号标记了将要传递给chmod的参数的结尾。 必须在其前面加上“ \”反斜杠来对其进行“转义”。

Once this command has been run, we can search for executable files as before, and this time there will be no files listed.

一旦运行了该命令,我们就可以像以前一样搜索可执行文件,这一次将没有文件列出。

Search results with no executable files ina terminal window

To cast our net wider, we could use a file pattern instead of the filename we used in our example.

为了扩大网络范围,我们可以使用文件模式代替示例中使用的文件名。

This flexibility allows you to search for specified file types, or with filename patterns, and have some action carried out on the matching files.

这种灵活性使您可以搜索指定的文件类型或文件名模式,并对匹配的文件执行某些操作。

Find has many other options, including searching for files by their modified date, files owned by a user or group, files that are readable, or files that have a specific set of file permissions.

查找具有许多其他选项 ,包括按修改日期搜索文件,用户或组拥有的文件,可读文件或具有特定文件权限集的文件。

find和mlocate命令 (The locate And mlocate Commands)

Many Linux distributions used to have a copy of locate included with them. This was superseded by the mlocate command, which was an improved and updated version of locate.

许多Linux发行版以前都包含一个locate副本。 mlocate命令取代了该命令,后者是locate的改进和更新版本。

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

在系统上安装了mlocate它将修改locate命令,以便即使您键入locate也可以实际使用mlocate

Current versions of Ubuntu, Fedora, and Manjaro were checked to see whether they had versions of these commands pre-installed on them. Ubuntu and Fedora both included mlocate. It had to be installed on Manjaro, with this command:

检查了Ubuntu,Fedora和Manjaro的当前版本,以查看它们是否已预先安装了这些命令的版本。 Ubuntu和Fedora都包含mlocate. 必须使用以下命令将其安装在Manjaro上:

sudo pacman -Syu mlocate

   
   
sudo pacman -Syu mlocate in a terminal window

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

在Ubuntu上,您可以互换使用locate和mlocate 。 在Fedora和Manjaro上,您必须键入locate ,但是该命令由mlocate执行。

If you use the  --version option with locate you’ll see that the command that responds is actually mlocate.

如果将--version选项与locate一起使用,您将看到响应的命令实际上是mlocate

locate --version
locate --version in a terminal window

Because locate works on all of the Linux distributions that were tested, we’ll use locate in our explanations below. And it’s one less letter to type.

因为locate适用于所有经过测试Linux发行版,所以我们将在下面的说明中使用locate 。 而且要键入的字母要少一个。

定位数据库 (The locate Database)

The biggest advantage that locate has is speed.

locate具有的最大优势是速度。

When you use the find command, it dashes off and performs a search across your filesystem. The locate command works very differently. It does a database lookup to determine whether what you are looking for is on your computer. That makes the search much faster.

使用find命令时,它会破折号并在整个文件系统中执行搜索。 locate命令的工作方式非常不同。 它执行数据库查找,以确定您要查找的内容是否在计算机上。 这样可以使搜索更快。

Of course, it does raise an obvious question about the database. What ensures the database is up to date? When mlocate is installed it (usually) places an entry in cron.daily. This runs each day (very early in the morning) and updates the database.

当然,这确实引起了有关数据库的明显问题。 是什么确保数据库是最新的? 安装mlocate ,(通常)将其放置在cron.daily 。 它每天(非常早)运行并更新数据库。

To check whether this entry exists, use this command:

要检查此条目是否存在,请使用以下命令:

ls /etc/cron.daily/*loc*
ls /etc/cron.daily/*loc* in a terminal window

If you don’t find an entry there, you could set up an automated task to do this for you at the time you choose.

如果您在此处找不到条目,​​则可以设置一个自动任务来在您选择的时间为您执行此操作。

What if your computer isn’t on at the time when the database is supposed to be updated? You can manually run the database update process with the following command:

如果在应该更新数据库时您的计算机没有打开怎么办? 您可以使用以下命令手动运行数据库更新过程:

sudo updatedb
sudo updatedb in a terminal window

使用定位 (Using locate)

Let’s look for files that contain the string “getlatlong”. With locate, the search automatically looks for any matches that contain the search term anywhere in the filename, so there is no need to use wildcards.

让我们查找包含字符串“ getlatlong”的文件。 使用locate,搜索会自动在文件名中的任何位置查找包含搜索词的所有匹配项,因此无需使用通配符。

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.

在屏幕截图中很难传达速度,但几乎立即为我们列出了匹配的文件。

locate results with files containing getlatlong in a terminal window

告诉定位您想要多少个结果 (Telling locate How Many Results You Want)

Sometimes you may know there are lots of files of the type your searching for. You only need to see the first few of them. Perhaps you just want to be reminded which directory they are in, and you don’t need to see all of the filenames.

有时,您可能会知道有很多搜索类型的文件。 您只需要查看其中的前几个即可。 也许您只是想提醒他们位于哪个目录中,而无需查看所有文件名。

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

使用-n (数字)选项,您可以限制locate的结果将返回给您的数目。 在此命令中,我们设置了10个结果的限制。

locate .html -n 10
locate .html -n 10 in a terminal window

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

locate通过列出从数据库中检索到的前10个匹配文件名进行响应。

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

计数匹配文件 (Counting Matching Files)

If you only want to know the number of matching files and you don’t need to know what they are called or where they are on your hard drive, use the -c (count) option.

如果只想知道匹配文件的数量,而又不需要知道它们的名称或它们在硬盘驱动器上的位置,请使用-c(计数)选项。

locate -c .html
locate -c .html in a terminal window

So, now we know there are 431 files with the “.html” extension on this computer. Maybe we do want to have a look at them, but we thought we’d take a peek and see how many there were first. Armed with that knowledge we know we’ll need to pipe the output through less.

因此,现在我们知道这台计算机上有431个扩展名为“ .html”的文件。 也许我们确实想看看它们,但我们认为我们先看看有多少个。 有了这些知识,我们知道我们需要通过less管道输出。

locate .html | less
locate .html | less in a terminal window

And here they all are, or at least, here’s the top of the long list of them.

在这里,它们都是,或者至少在这里是一长串。

html file listing piped through less in a terminal window

忽略带有定位的案例 (Ignoring Case With locate)

The -i (ignore case) causes locate to do just that, it ignores uppercase and lowercase differences between the search term and the filenames in the database. If we try and count the HTML files again, but mistakenly provide the search term in uppercase we’ll get zero results.

-i (忽略大小写)使locate做到这一点,它忽略了搜索词与数据库中文件名之间的大小写差异。 如果我们再次尝试对HTML文件进行计数,但错误地以大写形式提供了搜索字词,则结果将为零。

locate -c .HTML
locate -c .HTML in a terminal window

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

通过包含-i选项,我们可以使locate忽略大小写的差异,并返回此机器的预期答案,即431。

locate -c -i .HTML
locate -c -i .HTML in a terminal window

定位数据库状态 (The locate Database Status)

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

要查看数据库的状态,请使用-s (状态)选项。 这导致locate返回有关数据库大小和内容的一些统计信息。

locate -s
locate -s in a terminal window

哪个命令 (The which Command)

The which command searches through the directories in your path, and tries to locate the command you are searching for. It allows you to determine which version of a program or command will run when you type its name on the command line.

which命令搜索路径中的目录,并尝试找到要搜索的命令 。 它使您可以确定在命令行上键入程序命令的名称时将运行哪个版本。

Imagine we had a program called geoloc. We know it is installed on the computer, but we don’t know where it is located. It must be in the path somewhere because when we type its name, it runs.  We can use which to locate it with this command:

想象一下,我们有一个名为geoloc的程序。 我们知道它已安装在计算机上,但我们不知道它的位置。 它必须在路径中的某处,因为当我们键入它的名称时,它将运行。 我们可以通过以下命令使用which来定位它:

which geoloc
which geoloc in a terminal window

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

which报告程序位于/usr/local/bin

geoloc in /usr/local/bin

We can check whether there are any other copies of the program in other locations within the path by using the -a (all) option.

我们可以使用-a (all)选项检查路径中其他位置是否有该程序的其他副本。

which -a geoloc
which -a geoloc in a terminal window

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

这说明我们在两个地方都有geoloc程序。

which -a geoloc in a terminal window

Of course, the copy in /usr/local/bin is going to be found first by the Bash shell every time, so having the program in two places is meaningless.

当然,每次都会由Bash shell首先在/usr/local/bin中找到该副本,因此将程序放在两个位置是没有意义的。

Removing the version in /usr/bin/geoloc will save you a bit of hard drive capacity. More importantly, it will also avoid issues created by someone manually updating the program, and doing it in the wrong place. Then wondering why they don’t see the new updates when they run the program.

删除/usr/bin/geoloc的版本会节省一些硬盘容量。 更重要的是,它还可以避免有人手动更新程序并在错误的位置进行操作而造成的问题。 然后想知道为什么他们在运行程序时没有看到新的更新。

whereis命令 (The whereis Command)

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

whereis命令与which命令相似,但是它提供的信息更多。

In addition to the location of the command or program file, whereis also reports where the man (manual) pages and source code files are located. In most cases, the source code files won’t be on your computer, but if they are, whereis will report on them.

除了命令或程序文件的位置, whereis还报告手册(手册)页面和源代码文件的位置。 在大多数情况下,源代码文件将不会在您的计算机上,但如果是, whereis将有关报告。

The binary executable, the man pages and the source code are often referred to as the “package” for that command. If you want to know where the various components of the package for the diff command are located, use the following command:

二进制可执行文件,手册页和源代码通常称为该命令的“包”。 如果您想知道diff命令包的各个组件的位置,请使用以下命令:

whereis diff
whereis diff in a terminal window

whereis responds by listing the location of the diff man pages and the diff binary file.

whereis通过列出diff手册页和diff二进制文件的位置进行响应。

whereis resuts for diff in a terminal window

To restrict the results to only show the location of the binary (in effect, make whereis work like which ) use the -b (binary) option.

要限制结果以仅显示二进制文件的位置(实际上,使whereiswhich一样工作),请使用-b (二进制)选项。

whereis -b diff
whereis -b diff in a terminal window

whereis only reports on the location of the executable file.

whereis仅报告可执行文件的位置。

whereis output restricted to binary location only in a terminal window

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

要限制搜索仅在手册页上报告,请使用-m (手动)选项。 要限制搜索以仅报告源代码文件,请使用-s (源)选项。

To see the locations that whereis searches through, use the -l (locations) option.

要查看whereis搜索的位置,请使用-l (位置)选项。

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 the locations whereis will search in, we can, should we choose, restrict the search to a particular location or group of locations.

现在我们知道的位置whereis将在搜索,我们可以,我们应该选择,限制搜索的位置的特定位置或组。

The -B (binary list) option restricts the search for executable files to the list of paths provided on the command line. You must provide at least one location for whereis to search through. The -f (file) option is used to signal the end of the location last the start of the filename.

-B (二进制列表)选项将对可执行文件的搜索限制为命令行上提供的路径列表。 您必须提供至少一个位置whereis进行搜索。 -f (文件)选项用于表示文件名末尾位置的末尾。

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

whereis looks in the single place we asked to search through. That happens to be where the file is located.

whereis在我们要求搜索的单个位置中查找。 这恰好是文件所在的位置。

whereis results from using the -B option in a terminal window

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

您还可以使用-M (手动列表)选项将对手册页的搜索限制为您在命令行上提供的路径。 -S (源列表)选项允许您以相同的方式限制对源代码文件的搜索。

whatis命令 (The whatis Command)

The whatis command is used to quickly search through the man (manual) pages. It provides one-line summary descriptions of the term you’ve asked it to search for.

whatis命令用于快速搜索手册(手册)页面。 它提供了您要搜索的术语的单行摘要说明

Let’s start with a simple example. Although it looks like the starting point of deep philosophical debate, we’re just asking whatis to tell us what the term “man” means.

让我们从一个简单的例子开始。 虽然它看起来像深刻的哲学辩论的出发点,我们只是要求whatis告诉我们术语“人”的意思是什么。

whatis man
whatis man in a terminal window

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

whatis找到两个匹配的描述。 它为每个比赛打印简短描述。 它还列出了手册的编号部分,其中包含每个完整的说明。


   
   
whatis results in a terminal window

To open the manual at the section that describes the man command, use the following command:

要在描述man命令的部分打开手册,请使用以下命令:

man 1 man
man 1 man in a terminal window

The manual opens at section man(1), at the page for man.

该手册在部分人(1)打开,在该页面man

man page open at section one in a terminal window

To open the manual at section 7, at the page that discusses the macros you can use to generate man pages, use this command:

要打开第7节的手册,在讨论可用于生成手册页的宏的页面上,请使用以下命令:

man 7 man
man 7 man in a terminal window

The man page for the man macros is displayed for you.

将显示man宏的手册页。

man page open at section seven in a terminal window

搜索手册的特定部分 (Searching In Specific Sections of the Manual)

The -s (section) option is used to limit the search to sections of the manual you are interested in. To have the whatis search restricted to section 7 of the manual, use the following command. Note the quote marks around the section number:

-s (节)选项用于将搜索范围限制在您感兴趣的手册部分。要使whatis搜索仅限于手册的第7节,请使用以下命令。 请注意节号周围的引号:

whatis -s "7" man
whatis -s "7" man in a terminal window

The results only refer to section 7 of the manual.

结果仅参考手册第7节。

whatis results restricted to section seven in a terminal window

在通配符中使用whatis (Using whatis With Wildcards)

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

您可以将通配符与whatis一起使用。 您必须使用-w (通配符)选项来执行此操作。

whatis -w char*
whatis -w char* in a terminal window

The matching results are listed in the terminal window.

匹配结果在终端窗口中列出。

whatis wildcard matches in a terminal window

apropos命令 (The apropos Command)

The apropos command is similar to whatis, but it has a few more bells and whistles. It searches through the man page titles and one line descriptions looking for the search term. It lists the matching man page descriptions in the terminal window.

apropos命令与whatis相似,但是它还有更多的花哨功能 。 它搜索手册页标题和一行描述以查找搜索词。 它在终端窗口中列出了匹配的手册页描述。

The word apropos means “related to” or “concerning,” and the command apropos took its name from this.  To search for anything related to the groups command, we can use this command:

apropos一词的意思是“与…有关”或“有关”,而apropos命令的名称由此而来。 要搜索与groups命令相关的任何内容,我们可以使用以下命令:

apropos groups
apropos groups in a terminal window

apropos lists the results to the terminal window.

apropos将结果列出到终端窗口。

apropos results for group in a terminal window

使用多个搜索词 (Using More Than One Search Term)

You can use more than one search term on the command line. apropos will search for man pages that contain either of the search terms.

您可以在命令行上使用多个搜索词。 apropos将搜索包含两个搜索词之一的手册页。

apropos chown chmod
apropos chown chmod in a terminal window

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

结果如前所列。 在这种情况下,每个搜索词只有一个条目。

apropos results for chmod and chown in a terminal window.

使用完全匹配 (Using Exact Matches)

apropos will return man pages that contain the search term even if the term is in the middle of another word. To make apropos return only exact matches for the search term, use the -e (exact) option.

apropos将返回包含搜索词的手册页,即使该词在另一个词的中间。 要使apropos仅返回搜索词的完全匹配项,请使用-e (精确)选项。

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

为了说明这一点,我们将使用aproposgrep作为搜索词。

apropos grep
apropos grep in a terminal window

There are many results returned for this, including many where grep is incorporated in another word, such as bzfgrep.

为此返回了许多结果,包括许多将grep合并到另一个单词中的结果,例如bzfgrep

results for apropos grep in a terminal window

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

让我们再试一次,然后使用-e (精确)选项。

apropos -e grep
apropos -e grep in a terminal window

We have a single result this time, for what we were actually searching for.

这次我们只有一个结果,实际上是我们要寻找的结果。

results for apropos -e grep in a terminal window

匹配所有搜索词 (Matching All Search Terms)

As we saw earlier if you provide more than one search term apropos will search for man pages that contain either search term. We can change that behavior by using the -a (and) option. This makes apropos only select matches that have all of the search times in them.

如前所述,如果您提供多个搜索词,则apropos将搜索包含两个搜索词的手册页。 我们可以通过使用-a (和)选项来更改该行为。 这使得apropos仅选择其中具有所有搜索时间的匹配项。

Let’s try the command without the -a option so that we can see what results apropos gives.

让我们尝试不带-a选项的命令,以便我们可以看到apropos给出的结果。

apropos crontab cron
apropos crontab cron in a terminal window

The results include man pages that match one or the other of the search terms.

结果包括与其中一个搜索词匹配的手册页。

apropos results for crontab cron in a terminal window

Now we’ll use the -a option.

现在,我们将使用-a选项。

apropos -a crontab cron
apropos -a crontab cron in a terminal window

This time the results are narrowed down to those containing both search terms.

这次将结果缩小到包含两个搜索词的结果。

results for apropos -a crontab cron n a terminal window

还有更多选择 (Yet More Options)

All of these commands have more options—some of them many more options—and you are encouraged to read the man pages for the commands we’ve discussed in this article.

所有这些命令都具有更多选项,其中一些具有更多选项,我们鼓励您阅读本文中讨论的命令的手册页。

Here’s a quick summary for each command:

以下是每个命令的快速摘要:

  • find: Provides a feature rich and granular search capability to look for files and directories.

    find :提供丰富的功能和精细的搜索功能以查找文件和目录。

  • locate: Provides a fast database-driven search for programs and commands.

    位置 :提供快速的数据库驱动的程序和命令搜索。

  • which: Searches the $PATH looking for executable files

    其中 :搜索$ PATH以查找可执行文件

  • whereis: Searches the $PATH looking for executable files, man pages, and source code files.

    whereis :搜索$ PATH以查找可执行文件,手册页和源代码文件。

  • whatis: Searches the man one-line descriptions for matches to the search term.

    whatis :在人的单行描述中搜索与搜索词匹配的内容。

  • apropos: Searches the man page with more fidelity than whatis, for matches to the search term or terms.

    apropos :以比whatis更高的保真度来搜索手册页,以查找与搜索项或术语相匹配的内容。

Looking for more Linux terminal information? Here are 37 commands you should know.

寻找更多Linux终端信息? 这是您应该知道的37个命令

翻译自: https://www.howtogeek.com/425408/how-to-use-all-linuxs-search-commands/

linux 搜索命令

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值