Created
January 19, 2014 13:41
-
-
Save hell0again/8505154 to your computer and use it in GitHub Desktop.
bashでファイル名の一括置換のdryrun
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
## .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