Bash 批量重命名文件

Sim*_*mon 4 bash rename file-rename batch-rename

我在同一个目录中有一堆文件,名称如下:

IMG_20160824_132614.jpg

IMG_20160824_132658.jpg

IMG_20160824_132738.jpg

中间部分是日期,最后部分是照片拍摄时间。因此,如果我按名称对这些文件进行排序,结果将与按修改的日期/时间排序相同

我想使用 bash 将这些文件批量重命名为以下形式:

1-x-3.jpg

其中x代表文件在顺序排序中的位置(按名称/修改时间排序)

因此,上面的 3 个示例将重命名为:

1-1-3.jpg

1-2-3.jpg

1-3-3.jpg

是否有可以实现此目的的 bash 命令?还是需要脚本?

Joh*_*024 9

Try:

i=1; for f in *.jpg; do mv "$f" "1-$((i++))-3.jpg"; done
Run Code Online (Sandbox Code Playgroud)

For example, using your file names:

$ ls
IMG_20160824_132614.jpg  IMG_20160824_132658.jpg  IMG_20160824_132738.jpg
$ i=1; for f in *.jpg; do mv "$f" "1-$((i++))-3.jpg"; done
$ ls
1-1-3.jpg  1-2-3.jpg  1-3-3.jpg
Run Code Online (Sandbox Code Playgroud)

Notes:

  1. When expanding *.jpg, the shell lists the files in alphanumeric order. This seems to be what you want. Note, though, that alphanumeric order can depend on locale.

  2. The sequential numbering is done with $((i++)). Here, $((...)) represents arithmetic expansion. ++ simply means increment the variable by 1.