Skip to content

Instantly share code, notes, and snippets.

@caojianhua
Last active August 29, 2022 07:36
Show Gist options
  • Save caojianhua/c4399f282507e7f342660d37818c0e5d to your computer and use it in GitHub Desktop.
Save caojianhua/c4399f282507e7f342660d37818c0e5d to your computer and use it in GitHub Desktop.
Rename file to create date from exif info
#! /usr/bin/env bash
dir=$1
if [ -z "$dir" ]; then
echo "输入参数有误!"
exit
else
echo "正在处理$dir"
fi
files=$(ls "$dir"| tr " " "\?")
for filename in $files; do
filename=`echo $filename | tr "\?" " "`
f_path="$dir/$filename"
if [ ! -f "$f_path" ]; then
echo "$f_path 文件不存在!!!"
continue
fi
file_date="$(exiftool "$f_path" | grep 'Content Create Date' | awk -F ': ' '{print $2}' | sort | head -1)"
if [ -z "$file_date" ]; then
file_date="$(exiftool "$f_path" | grep 'Create Date' | awk -F ': ' '{print $2}' | sort | head -1)"
fi
if [ -z "$file_date" ]; then
echo "$f_path 未读取到日期"
continue
fi
new_name="$file_date"
# 所有的 : 替换为 -
# 所有的 . 替换为 空格
new_name="${new_name//:/-}"
new_name="${new_name//./ }"
case "$filename" in
*.PNG|*.png|*.JPG|*.jpg|*.JPEG|*.jpeg|*.AAE|*.HEIC)
new_name="照片 $new_name".${filename##*.}
;;
*.MOV|*.mov|*.MP4|*.mp4)
new_name="视频 $new_name".${filename##*.}
;;
esac
echo "rename $filename -> $new_name"
mv "$dir/$filename" "$dir/$new_name"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment