Skip to content

Instantly share code, notes, and snippets.

@hell0again
Created January 19, 2014 13:41
Show Gist options
  • Save hell0again/8505154 to your computer and use it in GitHub Desktop.
Save hell0again/8505154 to your computer and use it in GitHub Desktop.
bashでファイル名の一括置換のdryrun
## .JPGを.jpgに一括リネームしたい
# 単純に置換
/bin/ls -1 *.JPG | pern -npe '$_=~s/.JPG//' | xargs --replace {} mv {} {.JPG,.jpg}
# 長いし式があってるかよくわからないのでdryrunしたい
/bin/ls -1 *.JPG | perl -npe ' $_=~s/.JPG// ' | xargs --replace {} echo mv {}{.JPG,.jpg}
とか
/bin/ls -1 *.JPG | perl -nle 'printf("X=%s; echo mv \${X%.JPG}{.JPG,.jpg};\n", $_)'
# 1回実行する
$ /bin/ls -1 *.JPG | perl -nle 'printf("X=%s; echo mv \${X%.JPG}{.JPG,.jpg};\n", $_)'
X=test.JPG; echo mv ${X%.JPG}{.JPG,.jpg};
X=test2.JPG; echo mv ${X%.JPG}{.JPG,.jpg};
X=test3.JPG; echo mv ${X%.JPG}{.JPG,.jpg};
# bashに渡すと置換前後のファイル名が並ぶ
$ /bin/ls -1 *.JPG | perl -nle 'printf("X=%s; echo mv \${X%.JPG}{.JPG,.jpg};\n", $_)' | bash
mv test.JPG test.jpg
mv test2.JPG test2.jpg
mv test3.JPG test3.jpg
# 問題ないならさらにbashに渡す
$ /bin/ls -1 *.JPG | perl -nle 'printf("X=%s; echo mv \${X%.JPG}{.JPG,.jpg};\n", $_)' | bash | bash
$ /bin/ls -1
test.jpg
test2.jpg
test3.jpg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment