在 Linux 上查詢和刪除檔案和目錄


在本文中,我們將瞭解 Linux 中的 **find** 命令,以及如何使用 **find** 命令在 Linux 中刪除檔案和目錄。

find 命令

Linux 中的 **find** 命令是一個強大的命令列實用程式工具,它可以幫助您根據使用者指定的匹配模式搜尋、查詢或過濾檔案和目錄,並允許您對獲得的結果執行後續操作。這些操作可以是列印找到的檔案、刪除、讀取內容等。

檔案搜尋將從當前位置開始,並遞迴地進入層次結構中的所有目錄和子目錄。使用者可以透過提供所需的目錄或子目錄作為匹配模式來將搜尋限制到當前目錄的某個級別。

find 命令允許您按檔案、目錄、名稱、檔案或目錄的建立日期、修改日期、所有者和許可權進行搜尋。

語法

以下是 find 命令的語法:

$ find [path] [options] [expression]

引數

所有引數都是可選的,預設情況下 find 命令將獲取當前工作目錄並列出層次結構中存在的所有檔案。

  • **path** - 這裡 path 是您要搜尋檔案的路徑。

  • **options** - options 引數只是 find 命令搜尋檔案的模式。例如,它可以是檔案或資料夾的名稱、許可權、建立或修改時間或日期。

  • **expression** - 您要對結果執行的操作。例如 -delete、-print 等。表示式可以以不同的方式使用,我們將在下面的示例中詳細瞭解。

示例

在本例中,要執行刪除操作,我們將使用 -delete 表示式。假設您想從當前工作目錄中刪除一個 .txt 檔案,則相應的命令如下所示

find  .  -name filepattern  -delete

在上面的示例中,(.) 指的是當前工作目錄。

  • **-name** 選項用於獲取檔案模式,例如在我們的例子中,它可以是檔名稱,例如 test.txt,也可以是副檔名(“.txt”)。如果給定檔名,它將只刪除具有該名稱的檔案,如果給定副檔名,它將刪除該工作目錄中所有具有 .txt 的檔案。

  • **-delete** 表示式將執行檔案刪除操作。

讓我們在命令列中嘗試一下。

$ ls
demo/  img.jpg  img2.jpg  img3.jpg  img4.jpg  test.txt  xyz.txt

在當前目錄中,我有 demo/ 子目錄和檔案。讓我們嘗試從中刪除 test.txt。

localhost:~/testFindCommand# find . -name test.txt -delete

上述命令將刪除檔案,並且不會輸出任何內容。但是,如果您現在檢查資料夾,test.txt 將消失,如下所示。

localhost:~/testFindCommand# ls
demo      img.jpg   img2.jpg  img3.jpg  img4.jpg  xyz.txt

使用相同的命令,讓我們嘗試刪除所有具有 .jpg 副檔名的檔案。如果您檢視資料夾 demo/,您將獲得以下檔案

localhost:~/testFindCommand/demo# ls
abc.txt   img5.jpg

因此,當您嘗試刪除具有 .jpg 副檔名的檔案時,當前目錄和子目錄 demo 中的所有具有 .jpg 副檔名的檔案都將被刪除。讓我們執行命令並檢查結果。

localhost:~/testFindCommand# find . -name '*.jpg' -delete

現在,如果您檢查資料夾,您將獲得以下輸出:

localhost:~/testFindCommand# ls
demo     xyz.txt

在 demo/ 中,您將擁有以下詳細資訊

localhost:~/testFindCommand# ls
demo     xyz.txt
localhost:~/testFindCommand# cd demo
localhost:~/testFindCommand/demo# ls
abc.txt

因此,現在您的當前資料夾和子資料夾中將沒有任何 .jpg 檔案。

示例

在本例中,我們將刪除一個目錄。因此,讓我們瞭解可以使用哪些模式和選項來刪除目錄。

**-delete** 選項僅允許您刪除給定的目錄,前提是該目錄為空。如果目錄中存在任何檔案,它將丟擲一個錯誤。讓我們檢查一個示例。因此,在我們的當前工作目錄中,我們有以下檔案和子目錄。

localhost:~/testFindCommand# ls
demo      testDemo  xyz.txt

我們有一個 demo 和 testDemo 子目錄,讓我們檢查它們的內容

localhost:~/testFindCommand# cd demo
localhost:~/testFindCommand/demo# ls
abc.txt

demo 資料夾不為空,並且包含一個檔案 abc.txt。testDemo 資料夾為空,如下所示

localhost:~/testFindCommand# cd testDemo
localhost:~/testFindCommand/testDemo# ls
localhost:~/testFindCommand/testDemo#

現在,讓我們對 demo 和 testDemo 使用帶 -delete 的 find 命令。

find . -name demo -delete
Or,
find . -type d -name demo -delete //The -type d in above command is referred for directories.

上述命令不會刪除 demo 資料夾,並丟擲如下所示的錯誤

localhost:~/testFindCommand# find . -name demo -delete
find: ./demo: Directory not empty

現在,讓我們嘗試刪除空目錄 testDemo。

localhost:~/testFindCommand# find . -name testDemo -delete
localhost:~/testFindCommand# ls
demo     xyz.txt

空目錄已成功刪除,沒有任何問題。因此,請記住使用 -delete 選項來刪除空目錄。

示例

exec 選項允許您刪除目錄的所有內容。使用 -delete 選項遇到的問題將使用 -exec 選項解決。在詳細瞭解如何使用 -exec 命令之前,讓我們先了解如何使用它。

我們將使用以下表達式與 find 命令一起使用。

  • **-exec rm -rf {} \;** - 這允許您將 rm(刪除命令)與 exec 一起使用。目錄和子目錄中存在的所有檔案都將使用其中的 -rf 選項遞迴刪除。找到的檔案將放置在大括號 {} 佔位符中。

  • **-exec rm -rf {} +** - 此命令與上述命令大致相同,唯一的區別是:它在末尾使用了 + 號。與 (;) 相比,在查詢匹配檔案時,使用 + 的效能據說更好。此外,當您使用 (;) 時,您需要使用 (\) 對其進行轉義,而使用 + 時,您不需要對其進行轉義。

  • **-exec rm -rfi {} +** - 此命令也類似,但它具有 -i 選項,該選項代表互動式,它將在刪除檔案之前詢問您的許可。

讓我們用示例測試命令

-exec rm -rf {} \;

localhost:~/testFindCommand# find . -type d -name demo -exec rm -rf "{}" \;
find: ./demo: No such file or directory
localhost:~/testFindCommand# ls
xyz.txt

-exec rm -rf {} +

localhost:~/testFindCommand# find . -type d -name test -exec rm -rf "{}" +
localhost:~/testFindCommand#

-exec rm -rfi {} +

localhost:~/testFindCommand# find . -type d -name demo -exec rm -rfi "{}" +
rm: descend into directory './demo'? y
rm: descend into directory './demo/test'? y
rm: descend into directory './demo/test/abc'? y
rm: descend into directory './demo/test/abc/tt'? y
rm: remove directory './demo/test/abc/tt'? y
rm: remove directory './demo/test/abc'? y
rm: remove directory './demo/test'? y
rm: remove directory './demo'? y
localhost:~/testFindCommand#  

示例

您可以將 xargs 命令與從 find 命令獲得的結果一起使用以刪除目錄或檔案。刪除所有具有“*.txt”副檔名的檔案的命令

find . -name “*.txt” | xargs rm -rf
localhost:~/testFindCommand# ls
demo     xyz.txt
localhost:~/testFindCommand# find . -type f -name "*.txt"|xargs rm -rf
localhost:~/testFindCommand# ls
demo

使用 xargs 刪除目錄的命令。

localhost:~/testFindCommand# ls
demo     xyz.txt
localhost:~/testFindCommand# find . -name demo | xargs rm -rf
localhost:~/testFindCommand# ls
xyz.txt

更新於:2023-06-06

3K+ 瀏覽量

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.