Ying-sun Ho
Ying-sun Ho

Reputation: 1

moving files in subdirectories up one (and *only* one) level

i am trying to move files nested in subdirectories up exactly one level. i am in os x terminal, and am new to bash. i'm fairly certain this is simple, i just don't know how to do it.

i want to change a file structure that looks like this:

~/container
       /1-A
           LEVEL 1 - 000.jpg
           LEVEL 1 - 001.jpg
           LEVEL 1 - 002.jpg
       /1-B
           /2-A
               LEVEL 2 - 007.jpg
               LEVEL 2 - 008.jpg
               LEVEL 2 - 009.jpg
       /1-C
           LEVEL 1 - 003.jpg
           LEVEL 1 - 004.jpg
           LEVEL 1 - 005.jpg
           LEVEL 1 - 006.jpg
       /1-D
           /2-C
               LEVEL 2 - 010.jpg
               LEVEL 2 - 011.jpg
               LEVEL 2 - 012.jpg
               LEVEL 2 - 013.jpg
               LEVEL 2 - 014.jpg
       /1-E
           LEVEL 1 - 015.jpg
           LEVEL 1 - 016.jpg
           LEVEL 1 - 017.jpg
       /1-F
           /2-B
               /3-A
                   LEVEL 3 - 018.jpg
                   LEVEL 3 - 019.jpg
                   LEVEL 3 - 020.jpg
                   LEVEL 3 - 021.jpg

to one that looks like this:

~/container
       /1-A
           LEVEL 1 - 000.jpg
           LEVEL 1 - 001.jpg
           LEVEL 1 - 002.jpg
       /1-B
           LEVEL 2 - 007.jpg
           LEVEL 2 - 008.jpg
           LEVEL 2 - 009.jpg
           /2-A
       /1-C
           LEVEL 1 - 003.jpg
           LEVEL 1 - 004.jpg
           LEVEL 1 - 005.jpg
           LEVEL 1 - 006.jpg
       /1-D
           LEVEL 2 - 010.jpg
           LEVEL 2 - 011.jpg
           LEVEL 2 - 012.jpg
           LEVEL 2 - 013.jpg
           LEVEL 2 - 014.jpg
           /2-C
       /1-E
           LEVEL 1 - 015.jpg
           LEVEL 1 - 016.jpg
           LEVEL 1 - 017.jpg
       /1-F
           /2-B
               LEVEL 3 - 018.jpg
               LEVEL 3 - 019.jpg
               LEVEL 3 - 020.jpg
               LEVEL 3 - 021.jpg 
               /3-A

i have tried:

find ~/container  -mindepth 3 -type f -exec mv {} . \;

and

find ~/container  -mindepth 3 -type f -exec mv {} .. \;

but these move the files in relation to the root directory, not the directory the file itself is in. in other words, they move the files too far up. i want them to move up EXACTLY one level, however deeply nested they are to begin with.

can anyone help?

Upvotes: 0

Views: 3761

Answers (2)

user2350426
user2350426

Reputation:

This should solve your problem:

find ~/container -mindepth 3 -type f -execdir mv "{}" ./.. \;

Description:
find ~/container search inside this folder.
-mindepth 3 show only files that are 3 directories down.
-type f show only files (not directories).
-execdir execute the following command on each file inside its directory.
mv "{}" ./.. move the file one directory up.
\; repeat a new command for each file selected.

Upvotes: 6

Mahesh Kharvi
Mahesh Kharvi

Reputation: 399

find ~/container  -mindepth 3 -type f | xargs -i bash -c 'mv "{}" $(dirname "{}")/..'

For each file it finds dirname of it and moves it one level up.

** update **

with GNU find...

find ~/container  -mindepth 3 -type f  -execdir mv "{}" $(dirname "{}")/.. \;

while loop...

find ~/container  -mindepth 3 -type f | while read file; do
     mv "$file" "$(dirname "$file")/.."
done

Upvotes: 1

Related Questions