Skip to content

Instantly share code, notes, and snippets.

@kusa-mochi
Last active April 4, 2021 07:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kusa-mochi/b966debc70da616b9caedfaf8c2249c7 to your computer and use it in GitHub Desktop.
Save kusa-mochi/b966debc70da616b9caedfaf8c2249c7 to your computer and use it in GitHub Desktop.
ブログ投稿用に、画像・動画ファイルの一括リネーム、画像の一括リサイズ・EXIF削除を行うBashスクリプト
#!/bin/bash
#region usage
function usage {
cat <<EOF
$(basename ${0}) by.もち:
ブログ投稿用に、画像・動画ファイルの一括リネームを行うスクリプト。
引数で指定したディレクトリの直下にあるファイルのみが対象。
各メディアファイルに対して次の処理を行う。
・リネーム前のファイルに対しては上書きなどせず、新たに "renamed" フォルダを作成してその中にリネーム後のファイルを保存する。
・画像ファイルの場合は、一定のピクセルサイズ以内に収まるようにリサイズする。
・画像ファイルの場合は、EXIF情報を削除する。
Usage:
$(basename ${0}) <メディアファイルが保存してあるフォルダパス>
Options:
--help, -h このヘルプを表示する。
EOF
}
#endregion
#region 引数処理
case $1 in
--help | -h)
usage
exit 0
;;
*)
# 引数で指定したディレクトリが存在しない場合
if [ ! -d $1 ]; then
echo "ディレクトリが存在しません。" >&2
exit 1
fi
;;
esac
#endregion
#region 定数
startDir=`pwd`
wDir=$1
distDirName=renamed
imageExts=(
'jpg'
'jpeg'
'png'
'bmp'
'gif'
)
imageMaxSize="1024x768"
dateString=`date +%Y%m%d`
#endregion
#region 本チャンの処理
cd $wDir
mkdir $distDirName
idx=0
for file in `ls -F | grep -v /`
{
((idx++))
# 連番
num=`printf %03d $idx`
# リネーム後のファイル名(拡張子除く)
basename=${dateString}_${num}
# 元のファイルから拡張子を小文字で取得する。
ext=`echo $file | sed 's/^.*\.\([^\.]*\)$/\1/' | sed 's/.\+/\L\0/'`
# リネーム後のファイルのパス
outFile=./${distDirName}/${basename}.${ext}
# ファイルが静止画の場合
if [[ $(printf '%s\n' "${imageExts[@]}" | grep -qx $ext; echo -n $?) -eq 0 ]]; then
# 画像をリサイズ・リネーム・EXIF削除する。
convert -strip -resize $imageMaxSize $file $outFile
else
# ファイルをそのままリネームする。
cp $file $outFile
fi
echo "$file -> ./${distDirName}/${basename}.${ext}"
}
#endregion
#region 終了処理
# リネーム結果を表示する。
echo "${idx}個のファイルがリネームされました。(画像ファイルについてはリサイズ・EXIF削除されました)"
cd $startDir
echo "fin."
exit 0
#endregion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment