Shell-Script
根據文件名創建目錄樹
我有大量具有以下結構的文件:
[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
嘗試:
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