将多个目录中的文件重命名为目录名 [英] Rename files in multiple directories to the name of the directory
本文介绍了将多个目录中的文件重命名为目录名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的东西:
v_1/file.txt
v_2/file.txt
v_3/file.txt
...
,我想将这些文件重命名为这样的内容:
and I want to rename those files to something like this:
v_1.txt
v_2.txt
v_3.txt
...
在同一目录中.
我想我可以使用rename
,但是我不知道如何同时使用它和文件夹和文件重命名.
I guess I can use rename
but I can't figure out how to use it with folder and file renaming at the same time.
推荐答案
可以使用bash for loop和mv
来实现结果:
The result can be achieved with a bash for loop and mv
:
for subdir in *; do mv $subdir/file.txt $subdir.txt; done;
请注意,如果目录名称包含空格,则上述解决方案将不起作用.相关链接.
Note that the solution above will not work if the directory name contains spaces. Related link.
另一种基于注释的解决方案(也适用于名称中带有空格的目录):
Another solution based on comments (that works for directories having spaces in the name as well):
find . -type d -not -empty -exec echo mv \{\}/file.txt \{\}.txt \;
这篇关于将多个目录中的文件重命名为目录名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文