1

I have a large number of files with the following structure:

[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.

basically the files follow the follwoing pattern: [{animal}] {year} {location} {format}.{ext}

How can I move the file so they have a directory stucture like this?

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
0

Try:

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

result:

$ 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
  • It mostly works, but it but the book and magazine files in separate directories. I want them to be in the same directory. – megascrapper Apr 15 at 8:45
  • Just fyi, find -maxdepth sould be find . -maxdepth. At least that;s what I have to do on my computer. Btw thanks for the help – megascrapper Apr 16 at 2:48
  • @αғsнιη The point (i.e. a path) is optional on GNU's find. Other systems (for instance MacOS) require an explicit path. – LL3 Apr 16 at 16:26
  • ohhh, I don't know mac os, sry – αғsнιη Apr 16 at 16:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.