Linux has different tools to search and find files and directories. locate
is one of them which is very popular. The alternative for locate is find
command. But they work differently from each other. Find command searches in real time but locate command uses a database which holds all files and directories of the system. In this tutorial, we will look at how to use locate.
Linux有不同的工具来搜索和查找文件和目录。 locate
是其中之一,它非常受欢迎。 查找的替代方法是find
命令。 但是它们的工作方式互不相同。 查找命令实时搜索,但是查找命令使用一个数据库,该数据库包含系统的所有文件和目录。 在本教程中,我们将研究如何使用定位。
查找命令语法 (locate Command Syntax)
locate [OPTION]... PATTERN...
找到命令帮助 (locate Command Help)
We can list help information about the locate
command with the -h
option.
我们可以使用-h
选项列出有关locate
命令的帮助信息。
$ locate -h
更新定位数据库(Update locate Database)
As we stated in the introduction locate command uses a database to hold whole file system directories and files. This database is not updated automatically. So we should update the database to use more effectively. As there are some files requires root privileges to read and update database we should provide root privileges for the updatedb
command.
正如我们在简介中所述,locate命令使用数据库来保存整个文件系统目录和文件。 该数据库不会自动更新。 因此,我们应该更新数据库以更有效地使用。 由于有些文件需要root特权才能读取和更新数据库,因此我们应该为updatedb
命令提供root特权。
$ sudo updatedb
Generally, this update operation is not taking too much time in periodic updated. But if the update runs for the first time this may some more times to setup database.
通常,此更新操作不会花费太多时间进行定期更新。 但是,如果更新是首次运行,则可能需要更多时间来设置数据库。
查找位置 (Search With Locate)
Now we have updated our database we can search for some terms. We will use locate
command the provide the search term. In the example, we will search for a.txt
term.
现在,我们已经更新了数据库,我们可以搜索一些术语。 我们将使用locate
命令提供搜索词。 在示例中,我们将搜索a.txt
术语。
$ locate a.txt
As we can see from results the term a.txt
is not search for files it is simply looked in the full path string.
正如我们从结果中看到的那样,术语a.txt
不是搜索文件,它只是在完整路径字符串中查找。
仅显示现有结果 (Display Only Existing Results)
As we know locate command looks the database to find files and folders about the search term. What if the database is updated one year ago and there is a result those not exist anymore. Locate command has the ability to double-check if the result is existing currently. We will use -e
option to activate this feature.
众所周知,locate命令会在数据库中查找有关搜索词的文件和文件夹。 如果数据库在一年前进行了更新,结果又不存在该怎么办。 定位命令可以再次检查结果是否当前存在。 我们将使用-e
选项激活此功能。
$ locate -e a.txt
As we can see the first result /home/ismail/a.txt
not listed in the results because it is removed in the file system but the related record exists in the database. With -e
option we have corrected the result.
正如我们看到的那样,第一个结果/home/ismail/a.txt
未在结果中列出,因为它已在文件系统中删除,但相关记录存在于数据库中。 使用-e
选项,我们已经更正了结果。
极限结果 (Limit Results)
Some times there may be a lot of results which will fill our terminal. This is generally an unwanted situation. There is an option that will limit the results count that will be printed to the terminal. We will use -l
option with the number. In the example, we only want to list 5 results about search.
有时可能会有很多结果会填满我们的终端。 这通常是不希望的情况。 有一个选项将限制打印到终端的结果计数。 我们将-l
选项与数字一起使用。 在该示例中,我们只想列出5个有关搜索的结果。
$ locate -l 5 a.txt
仅显示结果计数(Display Only Result Count)
Some times we only interested in the count of results. This can be done in various ways with helper commands. But locate command all ready provides an option for this. We will use -c
option to only list the count of results.
有时我们只对结果计数感兴趣。 可以使用辅助程序命令以多种方式完成此操作。 但是所有准备就绪的locate命令为此提供了一个选项。 我们将使用-c
选项仅列出结果计数。
$ locate a.txt | wc -l
显示统计信息和数据库信息(Display Statistics and Database Information)
The database of locate command will hold a lot of records. We can get information about this like statistics with-S
option. this will list directories count, file count, size of file names, and the total size of the database.
locate命令的数据库将保存很多记录。 我们可以使用-S
选项获取有关此信息的信息,例如统计信息。 这将列出目录数量,文件数量,文件名大小以及数据库的总大小。
$ locate -S
显示定位命令版本(Display locate Command Version)
The version of locate command can be displayed with --version
option like below.
可以使用--version
选项显示locate命令的版本,如下所示。
$ locate --version
翻译自: https://www.poftut.com/locate-command-tutorial-with-examples-for-linux-to-find-files/