linux按名称查找文件
Linux provides different tools to find files for different attributes. We can find files according to their size, name, modification time location, etc. But one of the most used metrics is finding file according to its name. In this tutorial, we will look at different tools to find files by name in Linux.
Linux提供了不同的工具来查找具有不同属性的文件。 我们可以根据文件的大小,名称,修改时间位置等找到文件。但是最常用的指标之一是根据文件的名称查找文件。 在本教程中,我们将介绍在Linux中按名称查找文件的不同工具。
使用find命令查找文件 (Find Files with find Command)
find
is the most feature-full tool to find files according to its name. find
provides a lot more features which can be examined in the following tutorial.
find
是功能最齐全的工具,可根据其名称查找文件。 find
提供了更多功能,可以在以下教程中进行研究。
Linux Find Command With Examples
We will use -name
option which is used to specify the file name we search. In this example we will look file named db.conf
in this example.
我们将使用-name
选项,该选项用于指定我们搜索的文件名。 在此示例中,我们将在此示例中查找名为db.conf
文件。
$ find . -name db.conf
查找不区分大小写的文件(Find Files As Caseinsensitive)
In previous example we have searched in a case sensitive manner. Case sensitive will only list same case names. For example if we search for db.conf
it will only match db.conf
contained name but will not match DB.conf
or similar. We disable case sensitivity and match DB.conf
too with the -iname
as we ca see just prefixing with i
. In this example we will search caseinsenstive.
在前面的示例中,我们以区分大小写的方式进行搜索。 区分大小写只会列出相同的案例名称。 例如,如果我们搜索db.conf
,它将仅匹配db.conf
包含的名称,而不会匹配DB.conf
或类似名称。 我们禁用了区分大小写的功能,并且也将DB.conf
与-iname
因为我们看到的只是以i
DB.conf
。 在此示例中,我们将搜索不敏感的案例。
$ find . -iname db.conf
查找特定位置的文件 (Find Files For Specific Location)
We can also specify the location we can search. In previous example we have already specified the location by using dot which means the current working path. We can put other path. In this example, we will search in /home/ismail
我们还可以指定我们可以搜索的位置。 在前面的示例中,我们已经通过使用点指定了位置,这意味着当前的工作路径。 我们可以另辟path径。 在此示例中,我们将在/home/ismail
搜索
$ find /home/ismail -name db.conf
使用locate命令查找文件(Find Files Using locate Command)
Locate is a very fast and useful tool. locate will search its database and try to match the given term. locate will match the all name and path for the given term we can only look for the name with -b
option. This will only match file or folder name not the whole path. In this example we will search for db.conf
.
定位是一个非常快速且有用的工具。 定位将搜索其数据库并尝试匹配给定的术语。 查找将匹配给定术语的所有名称和路径,我们只能使用-b
选项查找名称。 这只会匹配文件或文件夹名称,而不匹配整个路径。 在此示例中,我们将搜索db.conf
。
$ locate -b db.conf
使用ls和更少的命令查找文件(Find Files Using ls and less Commands)
There is also a bit different and interesting way to search for files. ls command is used to list files and directories. We can also use ls recursively where we can list sub files and directories too. We will redirect the ls output to the less command. Then we will use less
search function which is run with /
to search in the output.
搜索文件还有另一种有趣的方式。 ls命令用于列出文件和目录。 我们还可以递归使用ls,在这里我们也可以列出子文件和目录。 我们将ls输出重定向到less命令。 然后,我们将使用less
搜索功能,该功能与/
一起运行,以在输出中进行搜索。
$ ls -R /home/ismail | less
then search db.conf
with the following command in the less screen.
然后在以下屏幕中使用以下命令搜索db.conf
。
/db.conf
linux按名称查找文件