ファイル名に基づいたディレクトリツリーの作成

ファイル名に基づいたディレクトリツリーの作成

次のような構造のファイルがたくさんあります。

[Lion] 2015 Africa Book.pdf
[Lion] 2015 Africa Magazine.pdf
[Lion] 2016 Africa Book.pdf
[Lion] 2016 Africa Magazine.pdf
[Lion] 2015 Asia Book.pdf
[Lion] 2015 Asia Magazine.pdf
[Lion] 2016 Asia Book.pdf
[Lion] 2016 Asia Magazine.pdf
[Tiger] 2016 Africa Book.pdf
[Tiger] 2016 Africa Magazine.pdf
[Tiger] 2015 Asia Book.pdf
[Tiger] 2015 Asia Magazine.pdf
[Tiger] 2016 Asia Book.pdf
[Tiger] 2016 Asia Magazine.pdf

など。

デフォルトでは、これらのファイルは次のパターンに従います。[{animal}] {year} {location} {format}.{ext}

このようなディレクトリ構造を持つようにファイルをどのように移動できますか?

Animal stuff
├── Lion
│   ├── 2015 - Africa
│   │   ├── [Lion] 2015 Africa Book.pdf
│   │   └── [Lion] 2015 Africa Magazine.pdf
│   ├── 2015 - Asia
│   │   ├── [Lion] 2015 Asia Book.pdf
│   │   └── [Lion] 2015 Asia Magazine.pdf
│   ├── 2016 - Africa
│   │   ├── [Lion] 2016 Africa Book.pdf
│   │   └── [Lion] 2016 Africa Magazine.pdf
│   └── 2016 - Asia
│       ├── [Lion] 2016 Asia Book.pdf
│       └── [Lion] 2016 Asia Magazine.pdf
└── Tiger
    ├── 2015 - Africa
    │   ├── [Tiger] 2015 Africa Book.pdf
    │   └── [Tiger] 2015 Africa Magazine.pdf
    ├── 2015 - Asia
    │   ├── [Tiger] 2015 Asia Book.pdf
    │   └── [Tiger] 2015 Asia Magazine.pdf
    ├── 2016 - Africa
    │   ├── [Tiger] 2016 Africa Book.pdf
    │   └── [Tiger] 2016 Africa Magazine.pdf
    └── 2016 - Asia
        ├── [Tiger] 2016 Asia Book.pdf
        └── [Tiger] 2016 Asia Magazine.pdf

答え1

努力する:

find . -maxdepth 1 -type f -exec bash -c '
    animal=${1%% *}; 
    year=${1#* }; year=${year% *};
    mkdir -p "${animal//[][]}/${year/ / - }" && mv "$animal $year"'*' "${animal//[][]}/${year/ / - }/"
' _ {} \; 2> /dev/null

結果:

$ tree
.
├── Lion
│   ├── 2015 - Africa
│   │   ├── [Lion] 2015 Africa Book.pdf
│   │   └── [Lion] 2015 Africa Magazine.pdf
│   ├── 2015 - Asia
│   │   ├── [Lion] 2015 Asia Book.pdf
│   │   └── [Lion] 2015 Asia Magazine.pdf
│   ├── 2016 - Africa
│   │   ├── [Lion] 2016 Africa Book.pdf
│   │   └── [Lion] 2016 Africa Magazine.pdf
│   └── 2016 - Asia
│       ├── [Lion] 2016 Asia Book.pdf
│       └── [Lion] 2016 Asia Magazine.pdf
└── Tiger
    ├── 2015 - Asia
    │   ├── [Tiger] 2015 Asia Book.pdf
    │   └── [Tiger] 2015 Asia Magazine.pdf
    ├── 2016 - Africa
    │   ├── [Tiger] 2016 Africa Book.pdf
    │   └── [Tiger] 2016 Africa Magazine.pdf
    └── 2016 - Asia
        ├── [Tiger] 2016 Asia Book.pdf
        └── [Tiger] 2016 Asia Magazine.pdf

関連情報