Created
August 31, 2017 02:53
-
-
Save seya128/770c7f9efc6d852f0aaa60945fd0a976 to your computer and use it in GitHub Desktop.
指定した階層以下のファイル名で使用出来ない文字を全角にしてリネームする
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# 指定した階層以下の使用出来ない文字を全角に変換する | |
# | |
# $ ./rename_unusabele_characters_to_zenkaku.sh [-n,--dry-run] <path> | |
# | |
# -n, --dry-run ドライランで実行する。変更されるファイルを確認出来ます。 | |
# 指定した<path>が存在しないとエラーになります。 | |
# | |
# [-n, --dry-run]指定をしない場合、必ず確認が入ります。 | |
# | |
# 追加機能 | |
# 指定した文字列の使用不可文字を全角に変換する | |
# $ ./rename_unusabele_characters_to_zenkaku.sh [--to-zenkaku] <string> | |
# | |
# 全角変換機能テスト | |
# 使用不可文字が入った文字列の変換結果を表示する | |
# $ ./rename_unusabele_characters_to_zenkaku.sh --test | |
# 指定した文字列に使用できない文字があれば、全角へ変換する | |
function UnusableCharactersToZenkaku () { | |
echo $1 |\ | |
sed -e 's/\\/\/g' |\ | |
sed -e 's/\////g' |\ | |
sed -e 's/:/:/g' |\ | |
sed -e 's/*/*/g' |\ | |
sed -e 's/?/?/g' |\ | |
sed -e 's/"/”/g' |\ | |
sed -e 's/</</g' |\ | |
sed -e 's/>/>/g' |\ | |
sed -e 's/|/|/g' | |
} | |
# 指定したパス以下の階層に使用出来ない文字が有れば、全角にリネームする | |
function Change () { | |
local target="$1" | |
local name="" | |
local c_name="" | |
echo "- $target" | |
for i in $(ls -1 "$target"); do | |
name="$i" | |
c_name=$(UnusableCharactersToZenkaku "$name") | |
if [ $name != $c_name ]; then | |
if [ $dryrun = $RUN ]; then | |
mv "$target/$name" "$target/$c_name" | |
name="$c_name" | |
fi | |
echo "Rename : $target/$name" | |
fi | |
if [ -d "$target/$name" ]; then | |
Change "$target/$name" | |
fi | |
done | |
} | |
function IsDryrun () { | |
if [ $1 = "-n" -o $1 = "--dry-run" ]; then | |
echo $DRYRUN | |
return 0 | |
fi | |
echo $RUN | |
return -1 | |
} | |
function SetTergetDirectory () { | |
if [ ! -e $1 ]; then | |
echo $1 : フォルダが存在しません | |
exit -1 | |
else | |
TARGET_PATH=$(cd "$1"; pwd) | |
return 0 | |
fi | |
} | |
################################################## | |
# ココからスタート | |
# テスト用コード | |
if [ $1 = "--test" ]; then | |
UnusableCharactersToZenkaku 'aaaa\aaaa' | |
UnusableCharactersToZenkaku 'aaaa/aaaa' | |
UnusableCharactersToZenkaku 'aaaa:aaaa' | |
UnusableCharactersToZenkaku 'aaaa*aaaa' | |
UnusableCharactersToZenkaku 'aaaa?aaaa' | |
UnusableCharactersToZenkaku 'aaaa"aaaa' | |
UnusableCharactersToZenkaku 'aaaa<aaaa' | |
UnusableCharactersToZenkaku 'aaaa>aaaa' | |
UnusableCharactersToZenkaku 'aaaa|aaaa' | |
exit 0 | |
elif [ $1 = "--to-zenkaku" ]; then | |
UnusableCharactersToZenkaku "$2" | |
exit 0 | |
fi | |
DRYRUN="dry-run" | |
RUN="normal-run" | |
IFS=" | |
" | |
dryrun=$(IsDryrun $1) | |
if [ $dryrun = $RUN ]; then | |
echo -n "実際に実行されます。よろしいですか? y/N > " | |
read INPUT | |
if [ "$INPUT" = "y" -o "$INPUT" = "Y" ]; then | |
echo "このまま実行します。" | |
else | |
echo "dry-run で実行します。" | |
dryrun=$DRYRUN | |
fi | |
SetTergetDirectory $1 | |
else | |
echo "$DRYRUN" | |
SetTergetDirectory $2 | |
fi | |
Change "$TARGET_PATH" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment