suntrop
suntrop

Reputation: 785

Batch move files into subfolders (recursively)

I've got

What I' like to get

At first it looks complicated. But all I want to do is move files in all folders just one folder down in the same folder (subfolders have to be created, great if it contains the name of its parent folder).

Is there any way I can do it with a bash command? Or an App? Hope you have some tipps for me :-)

Upvotes: 0

Views: 1404

Answers (1)

l4mpi
l4mpi

Reputation: 5149

This can be done in a bash one-liner with find (it's probably possible to make this easier/smaller, but i'm no shell guru):

find . -type f -exec bash -c 'mv {} $(dirname {})/$(basename $(dirname {}))-Subfolder/$(basename {})' \;`

(Find all non-directory files, move them from their original position to 'foldername/foldername-Subfolder/filename'. This has to be done with bash -c because the subshells would otherwise be evaluated first)

You need to create the subfolders before executing the command, which can be done with another find:

find . -type d ! -iname "*-Subfolder" -exec bash -c 'mkdir {}/$(basename {})-Subfolder' \;

Upvotes: 2

Related Questions