正規表現を使ってファイル名を変更する(sed)
#!/bin/sh #rename_sed.sh if [ $# -lt 3 ]; then echo "Usage: rename_sed.sh regexp replace files..." exit 1 fi #正規表現と置き換え文字列をセット regexp="$1" pattern="$2" shift 2 #残りの引数で指定されたファイルについて繰り返し処理 for orig do new=`echo "$orig" | sed s/"$regexp"/"$pattern"/` echo "$orig -> $new" mv "$orig" "$new" done
$ ls 2.text; ./rename_sed.sh '2' '1' *.text; ls 1.text 2.text 2.text -> 1.text 1.text