linux系统文件查找
In the last post we looked at the locate
command, although blazing fast but has few drawbacks.
在上一篇文章中,我们介绍了locate
命令,尽管速度很快,但缺点很少。
Today we are going to explore another command-line utility find
for searching files and directories in Linux System.
今天我们就来探讨另一个命令行实用程序find
在Linux系统来搜索文件和目录。
find
command
find
命令
The find
command searches through the file system based on simple pattern you specify and returns the result.
find
命令根据您指定的简单模式搜索文件系统并返回结果。
The syntax for find
command
find
命令的语法
![Image for post](https://miro.medium.com/max/9999/1*lBzK1itjTNqJa4i_HFYjFw.png)
To find anything, we just use find
command along with the simple_pattern we want to search
要查找任何内容,我们只需使用find
命令以及我们要搜索的simple_pattern
Let’s take an example where we want to find the contents of Documents directory
让我们举一个例子,我们要查找Documents目录的内容
![Image for post](https://miro.medium.com/max/9999/1*5l5RW_hYhktMMf2e0du7Xw.png)
In the above example, we used the command find ~/Documents
where
在上面的示例中,我们使用了命令find ~/Documents
其中
find
represents the find command ~/Documents
represents the simple pattern which specifies path till the Documents directory.
find
代表查找命令~/Documents
代表简单的模式,该模式指定到Documents目录的路径 。
In the above example we get the files as well as the directories for the specified pattern, but what if we only wanted files?
在上面的示例中,我们获取了指定模式的文件以及目录,但是如果我们只想要文件怎么办?
![Image for post](https://miro.medium.com/max/9999/1*uirjoiAieZaeZaTKEHS1AA.png)
In the above example we used the command find -type f
find
represents the find command -type
represents the "type" option which specifies the type of content f
specifies the content type "file"
在上面的示例中,我们使用命令find -type f
find
表示find命令-type
表示“ type”选项,该选项指定内容的类型f
指定内容类型“ file”
In the response, we got all the files present in the current working directory
在响应中,我们将所有文件显示在当前工作目录中
Okay, what about only getting directories in response
好吧,那只获取目录响应呢?
![Image for post](https://miro.medium.com/max/9999/1*nz11EIRLJ0m-3VMr6HOeMQ.png)
In the above example we used the command find -type d
where find
represents the find command -type
represents the "type" option which specifies the type of content d
specifies the content type "directory"
在上面的示例中,我们使用命令find -type d
,其中find
表示find命令-type
表示“ type”选项,该选项指定内容的类型d
指定内容类型“目录”
In the response we only got the directories .
i.e. current working directory and the logs
direcory.
在响应中,我们仅获得目录.
即当前工作目录和logs
目录。
This is all great but what about finding a file using filename?
一切都很好,但是使用文件名查找文件呢?
- find a file log1.txt 查找文件log1.txt
![Image for post](https://miro.medium.com/max/9999/1*FT4xcBuFWGo0eF6T2RJ3TA.png)
In the above example, we use the command find -name log1.txt
where find
represents the find command -name
represnts the option to find by name log1.txt
represents the name of the file to be found
在上面的示例中,我们使用命令find -name log1.txt
,其中find
代表查找命令-name
代表按名称 查找的选项log1.txt
代表要查找的文件的名称
Let’s take one more step ahead and get the files ignoring case
让我们再向前迈一步,让文件忽略大小写
- find files by name and ignore case 按名称查找文件并忽略大小写
![Image for post](https://miro.medium.com/max/9999/1*orM7CPfNmg0PJqHHYnwkzA.png)
In the above example, we used the command find -iname textFile1.txt
where find
represents the find command -iname
represents the option to find by name and ignore case textFile1.txt
represents the name of the file to be found
在上面的示例中,我们使用命令find -iname textFile1.txt
,其中find
代表查找命令-iname
代表按名称查找的选项,忽略大小写 textFile1.txt
代表要查找的文件的名称
These are all the basic scenarios covered with the find command.
这些是find命令涵盖的所有基本方案。
Now let’s look at some interesting real world scenarios
现在让我们来看一些有趣的现实世界场景
- Find all the files that are greater than or equal to 5mb 查找所有大于或等于5mb的文件
![Image for post](https://miro.medium.com/max/9999/1*-h65rptO1jfiG0Yz1k8eZw.png)
In the above example, we used the command find -type f -size +5M
where find
represents the find command -type f
represents the type of content which is file(f) in our case -size
represents the option to find files of required size +5M
represents the files that are greater than or equal to 5mb
在上面的示例中,我们使用命令find -type f -size +5M
,其中find
代表查找命令-type f
代表内容类型,在本例中为file(f) -size
代表用于查找所需文件的选项size +5M
表示大于或等于5mb的文件
find all the files that are greater than 2mb but less than 10mb
查找所有大于2mb但小于10mb的文件
![Image for post](https://miro.medium.com/max/9999/1*Yngi2va7skntVBMuWs0EUg.png)
In the above example, we use the command find -type f -size +2M -size -10M
where find
represents the find command -type f
represents the type of content which is file(f) in our case -size
represents the option to find files of required size +2M
represents the files that are greater than or equal to 2mb -10M
represents the files that are less than 10M
在上面的示例中,我们使用命令find -type f -size +2M -size -10M
,其中find
表示查找命令-type f
表示内容类型,在本例中为file(f) -size
表示查找所需大小为 +2M
的文件表示大于或等于2mb的文件-10M
表示小于10M的文件
What if we only wanted the zip files
如果我们只想要zip文件怎么办
- Find all the zip files 查找所有压缩文件
![Image for post](https://miro.medium.com/max/9999/1*i4IBX2OkC7RmS9H7FotClg.png)
In the above example, we used the command find -type f -name *.zip
where find
represents the find command -type f
represents the type of content which is file(f) in our case -name
represents the option to find files with specific name *.zip
represents all the zip files
在上面的示例中,我们使用命令find -type f -name *.zip
,其中find
代表查找命令-type f
代表内容类型,在本例中为file(f) -name
代表使用以下命令查找文件的选项特定名称 *.zip
表示所有zip文件
What if we wanted to find the files that were either greater than 2mb but less than 5M or files that were greater than or equal to 10mb
如果我们要查找大于2mb但小于5M的文件或大于或等于10mb的文件怎么办
- Find files with multiple conditions 查找具有多个条件的文件
![Image for post](https://miro.medium.com/max/9999/1*Tr69YZTaKg7pqt6m0IPu5Q.png)
In the above example, we used the command find -type f -size +2M -size -5M -o -size +10M
where find
represents the find command -type f
represents the type of content which is file(f) in our case -size
represents the option to find files of required size +2M
represents files greater than or equal to 2mb -5M
represents files less than 5mb -o
represents logical OR +10M
represents files greater than or equal to 10mb
在上面的示例中,我们使用命令find -type f -size +2M -size -5M -o -size +10M
,其中find
表示find命令-type f
表示内容类型,在本例中为file(f) -size
表示查找所需大小的文件的选项+2M
表示大于或等于-5M
文件-5M
表示小于5mb的文件-o
表示逻辑OR +10M
表示大于或等于10mb的文件
Simiarly we can use other Logical AND(-a), NOT(!) and so on
同样,我们可以使用其他逻辑AND(-a),NOT(!)等
What if we want to perform some operations after we find the files
找到文件后要执行一些操作怎么办
- find all the files greater than 2mb and less than 5mb and copy them to directory “files_filtered” 查找所有大于2mb小于5mb的文件,并将它们复制到目录“ files_filtered”
![Image for post](https://miro.medium.com/max/9999/1*bRvvQtRc4n-rrHtDVFPVXw.png)
In the above example, we used the command sudo find -type f -size +2M -size -5M -exec cp {} ~/files_filtered \;
where find
represents the find command -type f
represents the type of content which is file(f) in our case -size
represents the option to find files of required size +2M
represents files greater than or equal to 2mb -5M
represents files less than 5mb -exec
means execute command cp {}
represents command to execute on the output i.e. copy in our case ~/files_filtered
represents the path to output directory \;
represents that command has ended
在上面的示例中,我们使用了命令sudo find -type f -size +2M -size -5M -exec cp {} ~/files_filtered \;
其中find
代表查找命令-type f
代表内容类型,在本例中为file(f) -size
代表查找所需大小文件的选项+2M
代表文件大于或等于2mb -5M
代表文件小于5mb -exec
表示执行命令 cp {}
表示要在输出上执行的命令,即在我们的例子中,复制~/files_filtered
表示输出目录\;
的路径\;
表示命令已结束
But there is a problem with the above command, the exec
does not ask us before perfoming the operation which will work in case of copy but in case of move or delete this can create a lot of problems
但是上面的命令存在问题, exec
该操作之前exec
不会询问我们,该操作在复制的情况下将起作用,但是在移动或删除的情况下会产生很多问题
A safer alternative to -exec
for move and delete operation is -ok
-exec
进行移动和删除操作的更安全替代方法是-ok
- find all the files greater than 2mb and less than 5mb and move them to directory “files_filtered” 查找所有大于2mb小于5mb的文件,并将它们移到目录“ files_filtered”
![Image for post](https://miro.medium.com/max/9999/1*6Y_rozHJQ8E9jdKcepdFpw.png)
In the above example, we used the command sudo find -type f -size +2M -size -5M -ok mv {} ~/files_filtered \;
where
在上面的示例中,我们使用了命令sudo find -type f -size +2M -size -5M -ok mv {} ~/files_filtered \;
哪里
find
represents the find command -type f
represents the type of content which is file(f) in our case -size
represents the option to find files of required size +2M
represents files greater than or equal to 2mb -5M
represents files less than 5mb -ok
means execute command with check mv {}
represents command to execute on the output i.e. move in our case ~/files_filtered
represents the path to output directory \;
represents that command has ended
find
代表查找命令-type f
代表内容类型,在本例中为file(f) -size
代表查找所需大小文件的选项+2M
代表大于或等于-5M
文件-5M
代表小于5mb的文件-ok
表示带有check mv {}
执行命令表示要在输出上执行的命令,即在我们的情况下, ~/files_filtered
表示输出目录\;
的路径\;
表示命令已结束
Okay, so that’s all we need to know about find
command.
好的,这就是我们需要有关find
命令的全部信息。
I hope you understood find command-line utility in Linux. Please, let me know if there are any questions and suggestions on what you want us to explore next.
我希望您理解Linux中的find命令行实用程序 。 请让我知道您对我们接下来要探索的内容有任何疑问和建议。
翻译自: https://medium.com/swlh/finding-files-and-directories-in-linux-system-b38b47b858ec
linux系统文件查找