あら、これはかなり素敵! 多数のファイルをリネームしようと思ったらどうするのが良いでしょう。決まった形であればfind/xargsを使うのが簡単でしょう。しかし正規表現を使ったり、複数のパターンがあると若干面倒です。 そこで紹介したいのがmassrenです。ファイル名一覧を編集する形でリネーム処理が行える優れものです。

massrenのインストール

Mac OSXの場合は下記のコマンドです。

curl -O https://raw.github.com/laurent22/massren/master/install/install.osx.sh
chmod 755 install.osx.sh 
sudo ./install.osx.sh

Linuxの場合はこちら。

curl -O https://raw.github.com/laurent22/massren/master/install/install.linux-amd64.sh
chmod 755 install.linux-amd64.sh 
sudo ./install.linux-amd64.sh

Windowsの場合はバイナリが公開されています。

massrenの使い方

まずエディタを設定します。vimやemacsなど自由に選べます。

$ massren --config editor emacs
massren: Config has been changed: "editor" = "emacs"

実行前のファイル構成です。

例えばこんな感じにファイルがあったとします。そのディレクトリまで移動して、massrenコマンドを実行します。

そうすると設定したエディタが立ち上がって、ファイルの編集ができるようになります。

自由に編集できますので、正規表現を使ったりできます。

そして編集が終わったらエディタを終了します。そのタイミングでリネーム処理が実行されます。

リネーム後

ファイルやフォルダを区別せずリネームが行われています。

オプションは以下のようになっています。

$ massren --help
Usage:
  massren [OPTIONS]

Application Options:
  -n, --dry-run  Don't rename anything but show the operation that would have been performed.
  -v, --verbose  Enable verbose output.
  -c, --config   Set a configuration value. eg. massren --config <name> [value]
  -u, --undo     Undo a rename operation. eg. massren --undo [path]
  -V, --version  Displays version information.

Help Options:
  -h, --help     Show this help message

Examples:

  Process all the files in the current directory:
  % massren	
  
  Process all the JPEGs in the specified directory:
  % massren /path/to/photos/*.jpg
  
  Undo the changes done by the previous operation:
  % massren --undo /path/to/photos/*.jpg

  Set VIM as the default text editor:
  % massren --config editor vim

リネームというと専用のソフトウェアを使って正規表現で行うイメージが強かったですが、それを何度も繰り返すことを考えるとmassrenで一発で終わらせられるのは相当便利そうです。知っておいて損のないツールでしょう。

massrenはWindows/Mac OSX/Linux用、MIT Licenseのオープンソース・ソフトウェアです。

laurent22/massren