递归重命名在bash目录 [英] recursively rename directories in bash

查看:233
本文介绍了递归重命名在bash目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用的的替换字符串的一部分递归重命名包含字符串的的所有目录。我有这样的事情,到目前为止,但它不是做得比较工作。我也会喜欢的的要搜索的区分大小写

I'd like to recursively rename all directories containing the string foo by replacing that part of the string with Bar. I've got something like this so far, but it doesn't quite work. I'd also like foo to be searched case-insensitive.

找到。 ð型-exec bash的-c'MV$ 1$ {1 //富/酒吧}' - {} \\;

是否有任何优雅的单行,可能比这更好的尝试?其实我已经尝试了一些,但认为我会遵从专家的意见。注:我是Mac OS X系统上这样做,并没有这样的工具的改名安装

Are there any elegant one-liners that might be better than this attempt? I've actually tried a few but thought I'd defer to the experts. Note: i'm doing this on a Mac OS X system, and don't have tools like rename installed.

推荐答案

请尝试以下code。使用的参数扩展

Try the following code using parameter expansion

find . -type d -iname '*foo*' -depth -exec bash -c '
    echo mv "$1" "${1//[Ff][Oo][Oo]/BAr}"
' -- {} \;

但你最好的选择将是改名命令(有时名为改名

find . -type d -iname '*foo*' -depth -exec rename 's@Foo@Bar@gi' {} +

如果你使用的是 bash4 的zsh ** 意思的递归的):

shopt -s globstar
rename -n 's@Foo@Bar@gi' **/*foo*/

如果它适合您的需要,删除 -n (试运行)切换到重命名为真实的。

If it fit your needs, remove the -n (dry run) switch to rename for real.

部分DOC

重命名最初写的Perl的爸爸,Larry Wall的自己

rename was originally written by Perl's dad, Larry Wall himself.

这篇关于递归重命名在bash目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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