我有大量具有以下结构的文件:
[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
ETC。
基本上这些文件遵循以下模式:[{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