Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

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

share|improve this answer

Solution

find /src/dir -type f -exec mv --backup=numbered -t /dst/dir -- {} +

The command will find all regular files under /src/dir (including all subdirectories) and move them to the /dst/dir by use of the command mv. Just replace the directories by yours. Files with the same names will be renamed automatically.

Selecting files to move

If you want to move just MP3 files, add -iname "*.mp3" option to the find command after -type f.

Comparison to the reply by c0dev

Only the second command in the c0dev's reply answers the question. Below is how does it compare to this reply. The points 3. and 4. can be resolved in the other reply the same way as here.

  1. Contrary to -print0 -exec command {} + is a part of IEEE Std 1003.1 (POSIX), ISO/IEC 9945 and The Single UNIX Specification standards. Thus it should be more portable. See IEEE Std 1003.1, 2004 Edition, IEEE Std 1003.1, 2013 Edition and 0000243: Add -print0 to "find".
  2. Except mv the solution with -exec + does not need to call an additional command like xargs or parallel and hand over the file names twice.
  3. The other reply will silently overwrite files which have the same name. Here the files will be automatically renamed thanks to the option --backup=numbered. Unfortunately these backups with suffix like ~3~ will be hidden in most of the file manages by default. Unfortunately mv does not allow changing of the suffix but it could be easily post-processed by additional commands.
  4. The other reply can have unexpected behaviour when there are file names beginning with -. This is a possible security risk which is resolved here using the -- option.
share|improve this answer
    
Error: find: missing argument to `-exec' –  Chris Oct 12 '13 at 23:05
    
@Chris: You are right, it seems that {} must be as the last argument. Corrected. –  pabouk Oct 12 '13 at 23:27
    
@Chris: Does it work now as you wished? If yes, you can probably accept the answer so your question does not show up as "not-resolved". –  pabouk Oct 15 '13 at 9:58
    
This is what I am running, and getting directory-not-found errors: –  Chris Dec 30 '13 at 20:51
    
what I am running: clstal@clap:~$ ls 1st script.R Firefox_wallpaper.png part1vidproj zotero pdf manager Desktop mozilla.pdf Pictures Documents parent Videos clstal@clap:~$ find /source/directory -type f -exec mv -t /destination/directory {} + find: `/source/directory': No such file or directory –  Chris Dec 30 '13 at 21:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.