bash 重命名多个目录匹配模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18027799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 06:07:27  来源:igfitidea点击:

Rename multiple directories matching pattern

linuxbash

提问by carlspring

I would like to rename all directories under a basedir which match a name. For example:

我想重命名 basedir 下与名称匹配的所有目录。例如:

In basedir/, I have:

basedir/,我有:

- foo/bar/blah
- my/bar/foo
- some/bar/foo1
- other/foo/bar

I would like to rename all directories matching bar, but I would like to preserve the prefix part.

我想重命名所有匹配的目录bar,但我想保留前缀部分。

With find, I can easily make a list of all the directories like this:

使用find,我可以轻松地列出所有目录,如下所示:

find . -name repositoryunit -type d

However, how can I use -exec mv {} ...(or perhaps combine with another app) so that the prefix is preserved?

但是,我如何使用-exec mv {} ...(或者可能与另一个应用程序结合使用)以便保留前缀?

Many thanks in advance!

提前谢谢了!

回答by John Kugelman

find . -depth -name bar -type d -execdir mv {} baz \;

-execdirchanges directory to the parent before executing the command, so the mvhere will be local to each parent directory.

-execdir在执行命令之前将目录更改为父目录,因此mv此处将是每个父目录的本地目录。