微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

用相应的“* .bar”文件recursion删除所有“* .foo”文件

我怎样才能recursion地删除所有以.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将被删除,因为有兄弟file{1,3,4}.bar文件。 应该留下file{2,5}.foo留下这个结果:

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

Init.d脚本挂起

从Java调用脚本,接收到sigpipe信号

如何使用维护名称将eps文件转换为PNG文件

使用shell脚本使用sedreplacestring的特定字符

检查rsync命令是否运行成功

为什么从bash命令提示符运行时,GIT不使用core.editor和commit.templateconfiguration值?

在awk与Umlauts printf不工作

$ *在shell脚本中的含义是什么?

Bash,执行命令,但继续交互式会话

如果两台机器中的文件都是错误的,如何退出shell脚本?

请记住在尝试使用find和rm命令之前首先进行备份。

使用这个find :

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

while IFS= read -r FILE; do rm -f "${FILE%.bar}".foo done < <(exec find -type f -name '*.bar')

要么

find -type f -name '*.bar' | sed -e 's|.bar$|.foo|' | xargs rm -f

在bash 4.0及更高版本中,在zsh :

shopt -s globstar # Only needed by bash for f in **/*.foo; do [[ -f ${f%.foo}.bar ]] && rm ./"$f" done

在zsh ,只有当存在相应的.bar文件时,才能定义匹配以.foo结尾的文件的选择性模式,以便rm仅被调用一次,而不是每个文件一次。

rm ./**/*.foo(e:'[[ -f ${REPLY%.foo}.bar ]]':)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐