为大量图片重新命名

来源:互联网 发布:python pywin32 编辑:程序博客网 时间:2024/05/22 03:42
The easiest way of renaming image files in the current directory to our own filename with a

specific format is by using the following script:


#!/bin/bash
#Filename: rename.sh
#Description: Rename jpg and png files
count=1;
for img in *.jpg *.png
do
new=image-$count.${img##*.}                 #提取图片后序名,和新的名字结合为新的文件名
mv "$img" "$new" 2> /dev/null                   #如果mv的过程中出错,错误信息不显示, “2>”  错误信息重定向
if [ $? -eq 0 ];                                                  # "$?"  检查上一个执行命令成功与否,如果成功则返回0
then
echo "Renaming $img to $new"
let count++
fi

done


The output is as follows:
$ ./rename.sh
Renaming hack.jpg to image-1.jpg
Renaming new.jpg to image-2.jpg
Renaming next.jpg to image-3.jpg
The script renames all the .jpg and .png files in the current directory to new filenames in
the format image-1.jpg, image-2.jpg, image-3.jpg , image-4.png, and so on.