通过用另一个模式替换模式来递归重命名存储库和文件名

问题描述 投票:0回答:1

我想以递归方式重命名包含给定模式的特定目录下的所有存储库和文件名。

我想我用以下命令找到了一个潜在的解决方案here

find /your/target/path/ -execdir rename 's/special/regular/' '{}' \+

然而,似乎我没有正确的rename命令。

我安装了Perl Rename包,但我仍然没有基于Perl的rename命令。我该怎么做才能使用此命令?

这个rename解决方案有替代解决方案吗?

假设我有以下存储库和文件:

BOOK1/
BOOK1/BOOK1_summary.txt
BOOK1/BOOK1_chapter1/BOOK1_chapter1.txt

我想在存储库和文件名中重命名BOOK1的所有BOOK1:

BOOK1-01/
BOOK1-01/BOOK1-01_summary.txt
BOOK1-01/BOOK1-01_chapter1/BOOK1-01_chapter1.txt
bash rename file-rename batch-rename
1个回答
1
投票

您可以使用rename PERL实用程序,可以从this链接下载。

find  BOOK1 -depth -exec rename -n 's/^(.*)(BOOK1)(.*)$/$1BOOK1-01$3/' {} \;

干运输

rename(BOOK1/BOOK1_summary.txt, BOOK1/BOOK1-01_summary.txt)
rename(BOOK1/BOOK1_chapter1/BOOK1_chapter1.txt, BOOK1/BOOK1_chapter1/BOOK1-01_chapter1.txt)
rename(BOOK1/BOOK1_chapter1, BOOK1/BOOK1-01_chapter1)
rename(BOOK1, BOOK1-01)

验证干运行结果后,从-n中删除rename选项。

实际产出

 ls -R BOOK1-01/ # Used a recursive listing here.
BOOK1-01/:
BOOK1-01_chapter1  BOOK1-01_summary.txt

BOOK1-01/BOOK1-01_chapter1:
BOOK1-01_chapter1.txt

注意:带有find的-depth选项是关键。 find manpage说它在目录本身之前处理每个目录的内容。

© www.soinside.com 2019 - 2024. All rights reserved.