[英]Bash - Find several specific folders in a directory and its sbdirectories and rename these specific folders
I just started to study Bash. 我刚开始学习Bash。 I want to do a script to find some specific folders in a directory and its subdirectories and if it exist, rename it into the same folder where we have found it.
我想做一个脚本来查找目录及其子目录中的某些特定文件夹,如果存在,请将其重命名为我们找到它的相同文件夹。 The same specific folder can be in some subdirectories.
相同的特定文件夹可以位于某些子目录中。
I use this: 我用这个:
file=`find . -name a`
if [ -d $file ]
then
rename 's/a/b/' $file
fi
But don't work. 但是不行。 Is there anyway to do this process?
反正有这个过程吗?
Thanks. 谢谢。
Finally, i solved the problem with this: 最后,我解决了这个问题:
find . -name "a" -type d -execdir rename 's/a/b/' {} \; &>/dev/null
You can do this with oneliner: 您可以使用oneliner做到这一点:
find . -name "a" -type d -execdir rename 's/a/b/' {} \\;
The parameter to name might be regex. 要命名的参数可能是正则表达式。 With
-type d
it will find all directories. 使用
-type d
,它将找到所有目录。 -execdir
changes to a matching item's directory and then executes the rename
command, passing the filename of the item at hand as an argument ( {}
). -execdir
切换到匹配项的目录,然后执行rename
命令,将当前项的文件名作为参数( {}
)传递。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.