如何在for循环中重命名文件
我正在运行 Perl 脚本并尝试完成重命名文件,如下所示。
我的文件夹中有一个 *.ru.jp
文件列表,其中包含其他不相关的文件。我想用我作为计数器变量获得的数字进行重命名。
在 Bash 中,我会这样做:
for i in $(ls *.ru.jp); do x=${i%%.*}; mv $i "$x"t"$counter".ru.jp ;done
例如,如果计数器为 1,myfile.ru.jp
将被重命名为 myfilet1.ru.jp
。“t”只是表示 t1、t2...等的命名。最重要的是有一个外部循环,随着计数器变量的增加,最终将标记 mafilet2.ru.jp
等等。
我想知道如何编写和表示与 Perl 脚本中类似的 for 循环?
I am running a Perl script and trying to accomplish renaming files as below.
I have a list of *.ru.jp
files in a folder with other non-related files. I would like to rename with a number which I have got as a counter variable.
In Bash, I would do it as:
for i in $(ls *.ru.jp); do x=${i%%.*}; mv $i "$x"t"$counter".ru.jp ;done
E.g., myfile.ru.jp
would be renamed as myfilet1.ru.jp
if the counter is 1. "t" is just a naming to indicate t1,t2...etc. And there is an outer loop above all which eventually will label mafilet2.ru.jp
and so on as the counter variable increases.
I would like to know how could I write and represent a similar for loop as in a Perl script?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用 Perl 的文件 glob 和内置 rename 函数,如下所示:
You could use Perl's file glob and built-in rename function as follows:
我尝试了这个,它似乎可以完成工作:
I tried this and it seems to do the job: