Linux下刪除所有符合條件的檔案

2021-09-02 02:34:14 字數 684 閱讀 9313

linux系統下,刪除當前路徑中所有檔名以"test"(必須是英文狀態下的雙引號或者單引號)開頭的檔案,當前目錄可以用 . 表示,也可以用 ./ 表示

如當前處於/home/downloads/, 當前資料夾中檔案列表如下:

test01.bin

test02.txt

test03.csv

方法1:

ute:~/downloads$ find . -name 'test*' | xargs   # 返回符合條件的檔案列表

ute:~/downloads$ find . -name 'test*' | xargs rm -rf  # 刪除當前資料夾中的所有以bts開頭的檔案

ute:~/downloads$ find . -maxdepth 3 -name 'test*' | xargs rm -rf  # 指定搜尋的深度為3層, 注意當前目錄屬於第一層

方法2:

ute:~/downloads$ rm -rf $(find ./ -name 'test*')

ute:~/downloads$ rm -rf $(find ./ -maxdepth 2 -name 'test*')

一定要注意,如果是直接複製網上的命令,請確保對應的雙引號或者單引號為英文狀態下的,否則會出錯。

linux刪除符合條件的檔案

找到根目錄下所有的以test開頭的檔案並把查詢結果當做引數傳給rm rf命令進行刪除 1 find name test xargs rm rf 2 find name test exec rm rf 3 rm rf find name test 如果想指定遞迴深度,可以這樣 1 find maxde...

linux查詢符合條件的檔案並刪除

找到根目錄下所有的以test開頭的檔案並把查詢結果當做引數傳給rm rf命令進行刪除 1 find name test xargs rm rf 2 find name test exec rm rf 3 rm rf find name test 如果想指定遞迴深度,可以這樣 1 find maxde...

linux查詢符合條件的檔案並刪除

找到根目錄下所有的以test開頭的檔案並把查詢結果當做引數傳給rm rf命令進行刪除 1 find name test xargs rm rf 2 find name test exec rm rf 3 rm rf find name test 如果想指定遞迴深度,可以這樣 1 find maxde...