unix和linux命令
The Linux find command comes in handy when looking for files directly from the command line. The find command is given search criteria such as permissions, ownership, modification, size, time, and date among others to locate the file or directory in question.
直接从命令行查找文件时,Linux find命令会派上用场。 为find命令提供搜索条件,例如权限,所有权,修改,大小,时间和日期等,以查找有问题的文件或目录。
The find command is available in all Linux distros by default, therefore, there’s no need of installing special packages to use it. Due to its significance, the find command is an essential command to learn if you want to know more about the command line navigations on any Linux distribution.
默认情况下,find命令在所有Linux发行版中均可用,因此,无需安装特殊软件包即可使用它。 由于其重要性,find命令是学习如果您想进一步了解任何Linux发行版上的命令行导航的必要命令。
We will highlight some of the find command examples and explain the various options that you can use.
我们将重点介绍一些find命令示例,并说明您可以使用的各种选项。
Syntax
句法
$ findlocation comparison-criteria search term
列出当前目录中的文件 (Listing files in the current directory)
To list all files in a directory including files inside folders, run the command below.
要列出目录中的所有文件,包括文件夹内的文件,请运行以下命令。
$ find .
Sample output
样品输出
在指定目录中搜索文件 (Searching files within a specified directory)
If you want to search all files in a given directory, use the find command as follows
如果要搜索给定目录中的所有文件,请使用以下find命令
$ find directory_name
For example, to search for all files in /boot execute the command
例如,要搜索/ boot中的所有文件,请执行以下命令
$ find /boot
Sample output
样品输出
使用指定目录中的文件名搜索文件 (Searching files using the filename within a specified directory)
If you want to specify the search criteria using the name of the file in a directory, the syntax will be as follows
如果要使用目录中文件的名称指定搜索条件,则语法如下
$ find directory_name -name "file_name"
For example, to search for Apache2 files in /etc directory run
例如,要在/ etc目录中搜索Apache2文件,请运行
$ find /etc -name "apache2"
Output
输出量
递归查找具有指定文件扩展名的所有文件 (Recursively find all files with a specified file extension)
If you want to search for particular files bearing a specific extension, in a given directory, the syntax will be as follows
如果要在给定目录中搜索带有特定扩展名的特定文件,语法将如下所示
$ find directory_name -name "*.extension"
For example, to search for all configuration files (.conf) in /etc directory, execute
例如,要搜索/ etc目录中的所有配置文件(.conf),请执行
$ find /etc -name "*.conf"
Sample output
样品输出
限制搜索深度 (Limiting depth of search)
You can decide to limit the depth of your file search in directories. For example, if you want to limit your file search to the first level of the directory, the syntax will be
您可以决定限制目录中文件搜索的深度。 例如,如果您想将文件搜索限制为目录的第一级,则语法为
$ find directory_name -maxdepth 1 -name "*.conf"
So, if you want to limit the file search to the first level directory in /etc for files with .conf
extension execute:
因此,如果要将文件搜索限制在/ etc中的第一级目录中,以查找扩展名为.conf
文件,请执行:
$ find /etc -maxdepth 1 -name "*.conf"
Sample output
样品输出
As seen in the output above, the file search is limited to the /etc directory level. If you want to perform a more intensive search and go deeper within other directories within the /etc directory, increase the maxdepth value.
如上面的输出所示,文件搜索仅限于/ etc目录级别。 如果要执行更深入的搜索并在/ etc目录中的其他目录中更深入地查找,请增加maxdepth值。
For instance, to search for files with .conf extension up to the 3rd directory run
例如,要搜索扩展名为.conf的文件直到第3个目录,请运行
$ find /etc -maxdepth 3 -name "*.conf"
Sample output
样品输出
As seen from the above output, the search goes up to the 2nd and 3rd directories.
从上面的输出中可以看到,搜索将转到第二和第三目录。
反转搜索结果 (Invert search results)
You can also search for files that do not meet given criteria with the find command. This mode is helpful when you want to eliminate known files from the search pattern.
您也可以使用find命令搜索不符合给定条件的文件。 当您要从搜索模式中删除已知文件时,此模式很有用。
To do this, use the -not -name
attribute as shown
为此,请使用-not -name
属性,如下所示
$ find /etc -maxdepth 1 -not -name "*.conf"
Sample output
样品输出
The above output prints all the files that do not have the .conf fie extension.
上面的输出显示所有不具有.conf fie扩展名的文件。
在OR运算符中使用find (Using find with OR operator)
You can choose to combine search results with find by using the OR operator which is symbolized by -o
flag shown in the example below
您可以选择使用OR运算符将搜索结果与find结合使用,该运算符由以下示例中显示的-o
标志表示
$ find /etc -maxdepth 3 -name "cron" -o -name "ssh"
Sample output
样品输出
The above command searches for files bearing the name ssh OR cron in the </etc directory
上面的命令在</ etc目录中搜索名称为ssh或cron的文件
仅搜索文件或目录 (Searching for files only or directories only)
If you want to search for files only, use the - type f
attribute as shown in the example below
如果只想搜索文件,请使用- type f
属性,如下例所示
$ find /etc -type f -name "ssh"
Sample output
样品输出
If you want to search for directories only, use the - type d
attribute as shown in the example below.
如果只想搜索目录,请使用- type d
属性,如下例所示。
$ find /etc -type d -name "ssh"
Sample output
样品输出
搜索特定用户拥有的文件 (Searching for files owned by a particular user)
To search for files owned by a particular user in a specific directory, use the syntax:
要在特定目录中搜索特定用户拥有的文件,请使用以下语法:
$ find /path -user username
For instance, to find files owned by user james in /home
directory run the command below
例如,要在/home
目录中查找用户james拥有的文件,请运行以下命令
$ find /home -user james
Sample output
样品输出
搜索具有特定文件权限的文件 (Searching for files with certain file permissions)
To search for files with specific file permissions, use the syntax below
要搜索具有特定文件权限的文件,请使用以下语法
$ find /directory_name -type f -perm value
For example, to search for files with permissions 755 in /etc directory, run:
例如,要在/ etc目录中搜索权限为755的文件,请运行:
$ find /etc -type f -perm 755
Sample output
样品输出
搜索具有特定文件大小或文件范围的文件 (Searching for files with certain files sizes or a range of files)
Linux find command also offers users a chance to search files according to their file sizes.
Linux find命令还为用户提供了根据文件大小搜索文件的机会。
搜索N尺寸的文件 (Search files of N size)
For example, to search for files which are 10kb run:
例如,要搜索10kb的文件,请运行:
$ find /etc -type f -size 10k
Sample output
样品输出
To search files greater than 10kb run
要搜索大于10kb的文件,请运行
find /etc -type f -size +10k
Sample output
样品输出
To search files less than 10kb run
要搜索小于10kb的文件,请运行
find /etc -type f -size -10k
Sample output
样品输出
摘要 (Summary)
That was a quick overview of the Linux find command examples. As already shown searching files and directories on the command line is very easy. Knowing how the command operates is an essential tool for all system administrators.
Feel free to try out the above find command examples and let us know how it went.
那是Linux find命令示例的快速概述。 如前所述,在命令行中搜索文件和目录非常容易。 对于所有系统管理员来说,了解命令的工作方式都是必不可少的工具。
随时尝试上面的find命令示例,让我们知道它的运行方式。
翻译自: https://www.journaldev.com/25686/find-command-in-linux-unix
unix和linux命令