0

I have directories of media structured as followed:

~ $ tree baz
baz
├── Ajin [Season 1]
│   ├── Ajin Demi-Human - 01 - A Topic That Has Nothing to Do with Us.mkv
│   ├── Ajin Demi-Human - 02 - Why Is This Happening to Me I Didn`t Do Anything Wrong!.mkv
...snip...
├── Btooom!
│   ├── Btooom! - 01 - Start.mkv
│   ├── Btooom! - 02 - The Bloodstained High School Girl.mkv
...snip...
└── Claymore [Dual Audio]
    ├── checksums.md5
    ├── Claymore - 01 - Great Sword.mkv
    ├── Claymore - 02 - The Black Card.mkv
    ├── Claymore - 03 - The Darkness in Paradise.mkv
...snip...

3 directories, 53 files

I need to transverse the baz directory and for each sub directory create a sub-sub directory named "Season 01" and place all files (and any directories) into the newly created sub-sub directory "Season 01".

Ex the above would become:

baz
├── Ajin [Season 1]
│   ├── Season 01
│       ├── Ajin Demi-Human - 01 - A Topic That Has Nothing to Do with Us.mkv
│       ├── Ajin Demi-Human - 02 - Why Is This Happening to Me I Didn`t Do Anything Wrong!.mkv
...snip...
etc

I was trying to use find to accomplish this, but I could not figure out how to structure the -exec command to create the "Season 01" directory, and move the contents of the directory to the new directory.

Thanks

2 Answers 2

0

All you need is a simple loop.

cur=$(pwd)
for i in baz/*
do
    cd "$i"
    mkdir season1
    mv *.mkv season1
    cd "$cur"
done
0

Using bash:

shopt -s dotglob
for i in baz/*/*; do
  mkdir -p "${i%/*}/Season 01"
  mv "$i" "${i%/*}/Season 01"
done

First enable dotglob to also match hidden files. Then loop over the files inside the subdirectories of baz and

  • Create the Season 01 directory if it doesn't yet exist. The parameter expansion ${i%/*} removes the filename and leaves the parent directories (e.g. baz/Ajin [Season 1]).
  • Move the current file "$i" into the season directory.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .