重命名一组文件为001,002,...在Linux [英] Renaming a set of files to 001, 002, ... on Linux

查看:254
本文介绍了重命名一组文件为001,002,...在Linux的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我本来一组的形式image_001.jpg,image_002.jpg,...

I originally had a set of images of the form image_001.jpg, image_002.jpg, ...

我通过他们去并删除一些。现在,我想剩下的文件重命名回image_001.jpg,image_002.jpg,...

I went through them and removed several. Now I'd like to rename the leftover files back to image_001.jpg, image_002.jpg, ...

是否有一个Linux命令,将做到这整齐?我熟悉命名,但不能看到任何订购这样的文件名。我在想,既然 LS * .JPG 列出文件顺序(有间隙),该解决方案将是那输出传递到一个bash循环还是什么?

Is there a Linux command that will do this neatly? I'm familiar with rename but can't see anything to order file names like this. I'm thinking that since ls *.jpg lists the files in order (with gaps), the solution would be to pass the output of that into a bash loop or something?

推荐答案

如果我理解正确,你如image_001.jpg,image_003.jpg,image_005.jpg,你要重命名为image_001.jpg,image_002.jpg,image_003.jpg。

If I understand right, you have e.g. image_001.jpg, image_003.jpg, image_005.jpg, and you want to rename to image_001.jpg, image_002.jpg, image_003.jpg.

编辑:这是修改后把临时文件在当前目录。作为Stephan202指出的,这可以使显著区别,如果温度是在不同的文件系统。为了避免撞上在循环的临时文件,现在经过图像*

This is modified to put the temp file in the current directory. As Stephan202 noted, this can make a significant difference if temp is on a different filesystem. To avoid hitting the temp file in the loop, it now goes through image*

i=1; temp=$(mktemp -p .); for file in image*
do
mv "$file" $temp;
mv $temp $(printf "image_%0.3d.jpg" $i)
i=$((i + 1))
done

这篇关于重命名一组文件为001,002,...在Linux的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆