如何找到特定目录中的所有.txt文件,删除它们并使用bash脚本将文件夹打包到archiv中?

时间:2017-06-28 08:38:37

标签: linux bash rm

我有这段代码,必须找到所有.txt文件并删除它们

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

这是对的吗?之后如何存档文件夹?

1 个答案:

答案 0 :(得分: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参数的使用情况