linux系统文件查找_在Linux系统中查找文件和目录

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

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

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

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

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

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

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

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

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

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

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

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

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系统文件查找

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值