Search code examples
bashmove

Bash - move all files from subdirectories to up folders


/volume1/TVPack/A/Folder1/Folder11/ --> files
/volume1/TVPack/A/Folder2/Folder22/ --> files
/volume1/TVPack/B/Folder3/Folder33/ --> files
(the list goes on)...

I want to move all files from Folder11/22 to /volume1/TVPack/A and all files from Folder33 to /volume1/TVPack/B etc ie, move 2 levels up

The code that I'm using is

find /volume1/TVPack/*/ -type f -mindepth 3 -exec mv -- {} /volume1/TVPack/*/ \;

But this put all the files only in folder B, instead of putting the respective files to A and the respectives files to B.

I'm doing this on a task scheduler, so I think that I can't use mv * .[^.]*

Does anybody know how to do it?


Solution

  • Use -execdir, which cd's to the directory each file is in before executing the command.

    find /volume1/TVPack/*/ -type f -mindepth 3 -execdir mv -- {} ../../ \;
    

    Even better, use + instead of \; to minimize the number of mv commands.

    find /volume1/TVPack/*/ -type f -mindepth 3 -execdir mv -t ../../ -- {} +