加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash – 递归删除除某些特定类型之外的所有文件

发布时间:2020-12-15 21:59:34 所属栏目:安全 来源:网络整理
导读:我想以递归方式删除某些文件夹中的所有文件,但扩展名为.gz的文件夹除外.通常我用 find /thepath -name "foo" -print0 | xargs -0 rm -rf 以递归方式删除/ thepath中名为“foo”的所有文件夹.但现在我想添加一个排除选项.怎么可能? 例如,文件夹结构看起来像
我想以递归方式删除某些文件夹中的所有文件,但扩展名为.gz的文件夹除外.通常我用

find /thepath -name "foo" -print0 | xargs -0 rm -rf

以递归方式删除/ thepath中名为“foo”的所有文件夹.但现在我想添加一个排除选项.怎么可能?

例如,文件夹结构看起来像

.hiddenfolder
 .hiddenfolder/bin.so
 arc.tar.gz
 note.txt
 sample

所以我想删除所有内容但保留arc.tar.gz

解决方法

查找并删除/ thepath下的所有文件,但名称匹配* .gz除外:

# First check with ls -l
find /thepath -type f ! -name '*.gz' -print0 | xargs -0 ls -l

# Ok: delete
find /thepath -type f ! -name '*.gz' -print0 | xargs -0 rm -vf

哦,并删除所有空的剩余目录:

find /thepath -type d -empty -print0 | xargs -0 rmdir -v

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读