天天看點

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

指令的文法

linux系統檔案查找_在Linux系統中查找檔案和目錄

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目錄的内容

linux系統檔案查找_在Linux系統中查找檔案和目錄

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?

在上面的示例中,我們擷取了指定模式的檔案以及目錄,但是如果我們隻想要檔案怎麼辦?

linux系統檔案查找_在Linux系統中查找檔案和目錄

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

好吧,那隻擷取目錄響應呢?

linux系統檔案查找_在Linux系統中查找檔案和目錄

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

linux系統檔案查找_在Linux系統中查找檔案和目錄

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

    按名稱查找檔案并忽略大小寫

linux系統檔案查找_在Linux系統中查找檔案和目錄

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的檔案

linux系統檔案查找_在Linux系統中查找檔案和目錄

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的檔案

linux系統檔案查找_在Linux系統中查找檔案和目錄

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

    查找所有壓縮檔案

linux系統檔案查找_在Linux系統中查找檔案和目錄

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

    查找具有多個條件的檔案

linux系統檔案查找_在Linux系統中查找檔案和目錄

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”

linux系統檔案查找_在Linux系統中查找檔案和目錄

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”

linux系統檔案查找_在Linux系統中查找檔案和目錄

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系統檔案查找