2017-06-28 49 views
-1

我有這樣的代碼,它必須找到所有.txt文件並刪除它們如何找到特定目錄中的所有.txt文件,將其刪除並使用bash腳本將文件夾壓縮到檔案庫中?

#!/bin/bash 
find /home/user_name -name "*.txt" | xargs rm 

它是正確的嗎?那我怎樣才能存檔文件夾?

+0

除非你瞭解的後果,不要使用'xargs'。 'find'有一個刪除選項:'find/home/user_name -name「* .txt」-delete「。用它!找出如何歸檔文件夾的最好方法是google「如何歸檔文件夾linux」 – hek2mgl

回答

1
#!/bin/bash 
find /home/user_name -type f -name "*.txt" -exec rm {} \; 
# for your specific use case, you could also do 
# find /home/user_name -type f -name "*.txt" -delete 
# as @hek2mgl pointed out. 
# You can have a more restricted search by using -type f 
# You can archive the folder using the zip command like below 
zip -r user_name.zip /home/user_name 

注:閱讀的zip手冊頁看到-r參數的用法

相關問題