Linux – How to move files out of nested subdirectories into another folder in ubuntu? (Trying to strip off many subfolders)

file managementfile-transferlinuxUbuntu

How do I move files and not directories into another folder/parent folder?

I have a folder structure that is extremely ugly, with some .mp3 files buried 6 levels deep in a sub-folder.

I want to end up with all of the files (mostly .mp3 but not all) in one directory, with no subdirectories at all, using Ubuntu.

Help?

Best Answer

There is a great answer in the askubuntu-QA.

To do so, Open a terminal and execute this command:

mv  -v ~/Downloads/* ~/Videos/

It will move all the files and folders from Downloads folder to Videos folder.


To Move all files, but not folders:

But, If you are interested to move all files (but not folders) from Downloads folder to Videos folder, use this command

find ~/Downloads/ -type f -print0 | xargs -0 mv -t ~/Videos

To move only files from the Download folders, but not from sub-folders:

If you want to move all files from the Downloads folder, but not any files within folders in the Download folder, use this command:

find ~/Downloads/ -maxdepth 1 -type f -print0 | xargs -0 mv -t ~/Videos

here, -maxdepth option specifies how deep find should try, 1 means, only the directory specified in the find command. You can try using 2, 3 also to test.

See the Ubuntu find manpage for a detailed explanation.

Source