[英]How to run mv command run recursively in Linux
My environment: Bash 3.5 LinuxRedhat 我的环境:Bash 3.5 LinuxRedhat
I'm using the following code to rename all of my files in a single directory. 我正在使用以下代码在单个目录中重命名所有文件。
for file in *.* ; do mv "$file" "Add_$file" ; done
Now, I want to rename my file recursively.And I don't know how to do. 现在,我想递归地重命名文件,而且我也不知道该怎么做。
The first ideas with find don't work, because {} returns ./ with the command: find的第一个想法不起作用,因为{}使用以下命令返回./:
find . -type f -name "*.*" -execdir mv {} Add_{} ";"
We need something, which removes the ./ in front but works in subdirectories too. 我们需要一些东西,它可以删除前面的./,但也可以在子目录中使用。
echo 'f=$(basename "$1"); mv "$f" Add_"$f";' > adhoc.sh
chmod a+x adhoc.sh
find . -type f -name "*.*" -execdir $PWD/adhoc.sh "{}" ";"
At least this works for gnu-find
. 至少这对
gnu-find
。 Other finds might not have an -execdir
command. 其他查找可能没有
-execdir
命令。
Thanks for skyking for pointing my error out. 感谢您指出我的错误。
For Linux, you might find a version of 'rename' in the repository, but normally it has to be installed and isn't part of the standard installation. 对于Linux,您可能会在存储库中找到“重命名”的版本,但是通常必须安装该版本,并且它不是标准安装的一部分。 If you often rename with the commandline, it is worth the effort.
如果您经常使用命令行重命名,则值得付出努力。 With rename , you specify a regex substitute command and can test it first:
使用rename ,您可以指定一个正则表达式替换命令,然后可以对其进行测试:
find . -type f -exec rename -n "s/(.*)/Add_\1/" {} ";"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.