LoginSignup
0
1

More than 1 year has passed since last update.

ディレクトリ配下の全てのファイルに接頭辞をつけるコマンド

Posted at

ディレクトリ配下のファイル冒頭に一律同じ接頭辞をつけたい

ファイル名の接頭辞でファイルの属性を簡易的に区別したい場合など、特定ディレクトリ内のファイル全てに接頭辞をつけたい場合がある。

元のディレクトリ構成
$ tree
.
├── featureA
│   ├── image1.png
│   ├── image2.png
│   ├── image3.png
│   ├── image4.png
│   └── image5.png
└── featureB
    ├── image10.png
    ├── image6.png
    ├── image7.png
    ├── image8.png
    └── image9.png
期待結果
$ tree
.
├── featureA
│   ├── a_image1.png
│   ├── a_image2.png
│   ├── a_image3.png
│   ├── a_image4.png
│   └── a_image5.png
└── featureB
    ├── b_image10.png
    ├── b_image6.png
    ├── b_image7.png
    ├── b_image8.png
    └── b_image9.png

前準備

renameコマンドをもちいるため、インストールされていない場合はインストールする。

brew install rename

コマンド

下記コマンドで接頭辞をつけることができる。

rename 's/^/{つけたい接頭辞}/' *

今回の例で言えば、featureAディレクトリに移動後、rename 's/^/a_/' *を実行することでfeatureAディレクトリ内の全てのファイルにa_を付与することができる。

コマンド実行例

元のファイル構成
$ ls
image10.png     image6.png      image7.png      image8.png      image9.png
接頭辞にb_をつける
rename 's/^/b_/' *
実施結果
$ ls
b_image10.png   b_image6.png    b_image7.png    b_image8.png    b_image9.png
0
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
0
1