递归删除所有与QUOT; * foo的"与对应的文件QUOT; *酒吧和QUOT;档 [英] Recursively deleting all "*.foo" files with corresponding "*.bar" files

查看:158
本文介绍了递归删除所有与QUOT; * foo的"与对应的文件QUOT; *酒吧和QUOT;档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何递归删除包含.foo 结尾的所有文件具有相同名称的同级文件,但的.bar ?例如,请考虑下面的目录树:

 
├──DIR
│├──DIR
││├──file4.bar
││├──file4.foo
││└──file5.foo
│├──file2.foo
│├──file3.bar
│└──file3.foo
├──file1.bar
└──file1.foo

在这个例子中 file.foo file3.foo file4.foo 会因为有兄弟文件{1,3,4}的.bar 文件被删除。 文件{2,5}包含.foo 应单独留在家中留下这样的结果:

 
├──DIR
│├──DIR
││├──file4.bar
││└──file5.foo
│├──file2.foo
│├──file3.bar
└──file1.bar


解决方案

记住,你尝试之前,先备份该找到 RM 命令。

使用此找到

 找到。 -name* .foo此时-execdir的bash -c'[[-f$ {吧1%*}。]]&放大器;&安培; RM$ 1' - '{}'\\;

How can I recursively delete all files ending in .foo which have a sibling file of the same name but ending in .bar? For example, consider the following directory tree:

.
├── dir
│   ├── dir
│   │   ├── file4.bar
│   │   ├── file4.foo
│   │   └── file5.foo
│   ├── file2.foo
│   ├── file3.bar
│   └── file3.foo
├── file1.bar
└── file1.foo

In this example file.foo, file3.foo, and file4.foo would be deleted since there are sibling file{1,3,4}.bar files. file{2,5}.foo should be left alone leaving this result:

.
├── dir
│   ├── dir
│   │   ├── file4.bar
│   │   └── file5.foo
│   ├── file2.foo
│   ├── file3.bar
└── file1.bar

解决方案

Remember to first take a backup before you try this find and rm command.

Use this find:

find . -name "*.foo" -execdir bash -c '[[ -f "${1%.*}.bar" ]] && rm "$1"' - '{}' \;

这篇关于递归删除所有与QUOT; * foo的"与对应的文件QUOT; *酒吧和QUOT;档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆