LoginSignup
2
1

More than 3 years have passed since last update.

正規表現でファイル名を一括置換

Last updated at Posted at 2020-04-28

環境 windows10 bash

やりたかったこと

hogehoge_Ch1_L1~L20.pdf のファイルリストから
hogehoge_Ch1_L8~L14-2.pdf のファイル名の Ch1を Ch2に置換

find

対象ファイルを正規表現でリストアップしてみる。
正規表現の () | {} にエスケープが必要なようで不便なので regextype は posix-egrep を指定。

$ find . -regextype posix-egrep -regex '.*L(8|9|10|11|12|13|14).*'
./hogehoge_Ch1_L10.pdf
./hogehoge_Ch1_L11.pdf
./hogehoge_Ch1_L12.pdf
./hogehoge_Ch1_L13.pdf
./hogehoge_Ch1_L14-1.pdf
./hogehoge_Ch1_L14-2.pdf
./hogehoge_Ch1_L8.pdf
./hogehoge_Ch1_L9.pdf

sed

findした結果をsedコマンドで置換してリネーム

$ find . -regextype posix-egrep -regex '.*L(8|9|10|11|12|13|14).*' | sed 'p;s/Ch1/Ch2/' | xargs -n2 mv

参考

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1