Linux provides different ways to find directories. Here we will look at how to find directories in a recursive way. In this tutorial, we will use commands like find and locate.
使用find命令查找目录和文件 (Find Directories and Files with find Command)
Find command is a popular command to used a lot of different purposes like find file and directory, take a backup, copy files. Actually find do not have these abilities it just runs commands over search results like copy and backup.
`LOCATION` is the path or location we will search in “ LOCATION”是我们将在其中搜索的路径或位置
`SEARCH_TERM` is the term which is the file or directory name we want to search“ SEARCH_TERM”是我们要搜索的文件或目录名称的术语
`ACTION` is optional which can take actions like print, delete, rename in the search results“ ACTION”是可选的,可以在搜索结果中执行打印,删除,重命名等操作
We will search for a directory bin in the root file system.
我们将在根文件系统中搜索目录bin。
$ find / -name bin
Find Command
查找命令
获取有关目录和文件的详细信息(Get Detailed Information about The Directories and Files)
We can get detailed information about found directories by using -ls parameter for the find command.
通过为find命令使用-ls参数,可以获取有关找到的目录的详细信息。
$ find / -name bin -ls
Get Detailed Information
获取详细信息
Here results will list the size of the directory permission of the directory, owner, last change date, etc.
这里的结果将列出目录的目录权限大小,所有者,上次更改日期等。
仅列出目录 (Only List Directories)
Up to now, we have searched for all files and directories. We can search for only directories by providing a type parameter with a directory specifier.
到目前为止,我们已经搜索了所有文件和目录。 我们可以通过为类型参数提供目录说明符来仅搜索目录。
$ find / -name bin -ls -type d
Only List Directories
仅列出目录
定位(Locate)
Locate command is a non-interactive alternative to find command. Also, locate have restricted capabilities. The advantage of the locate command is that it is fast because locate use database to search. Manually a database for file and directories is created. Manually this database is updated. The search is done directly in this database. Database is located at /var/lib/mlocate/mlocate.db.