I have a directory : ../Music with a lot of music files in a lot of subdirectories.

How would i move all the files in the subdirectories to another directory ?
Thus, i only want to move the files, i don't want to keep the subdirectory structure.


Assuming the current directory has the subfolders, replace TARGET_DIR in the following statement in use it:

find . -name '*.mp3' -exec mv {} TARGET_DIR \;
  • Nice, i knew it could be done by using find, but my knowledge of the commands in unix is somewhere burried deep inside. Thanks! – Aerus Jan 6 '11 at 10:57
  • @Aerus you're welcome. And not to sound pretentious, but when you know which command to use, you're only a "man command" away from knowing how to use it. – chris Jan 6 '11 at 12:27
  • Oh, i knew i could find all the files in the subdirectories with the find command, but i thought i then would just have list of them, which i then needed to process. I forgot about the exec, which is basically why i didn't check out the man page for find :) (Of course, if i would have checked it out, i would have found out about the exec ) – Aerus Jan 6 '11 at 14:39
  • @Aerus Give mywiki.wooledge.org/UsingFind a read. It'll give you a good idea about what other tasks find can be used for. – geirha Feb 29 '12 at 20:27
  • 1
    With GNU find you can avoid spawning a new mv process for each file to move: find ../Music \( -name \*.mp3 -o -name \*.ogg \) -exec mv -t [TARGET_DIR] -- \{\} +. Alternatively with POSIX find: find ../Music \( -name \*.mp3 -o -name \*.ogg \) -print0 | xargs -0 -- mv -t [TARGET_DIR] --. – David Foerster Dec 21 '14 at 0:27

Try this:

 cd ../Music

 for i in `ls -Q`; do  mv $i/* /Path/To/Some/Other/Directory;   done

I would suggest to use cp instead of mv to check whether it is working fine or not. and if it is working fine just delete the ..Music directory

Edit: added -Q (and needed to add this explanation to get beyond six characters)

  • I'll see if i can use cp (i don't know if i have enough space on my ubuntu partition because i believe it's 150GB+ of music :) ), thanks for the hint! – Aerus Jan 6 '11 at 10:59
  • Eeek. This is quite wrong actually as it doesn't consider blanks in filenames. I edited in a -Q as parameter to the ls ... – 0xC0000022L Feb 29 '12 at 18:18
  • 1
    Never parse ls output. Use a glob. See mywiki.wooledge.org/BashPitfalls – geirha Feb 29 '12 at 20:24

The first answer is correct, but an easier way to do it, assuming you only have one layer of subdirectories, is:

mv ../Music/*/*.mp3 [insert target dest here]
  • This could better be used as a comment. – Virusboy Dec 21 '14 at 0:27
  • I don't have enough reputation to comment on others' posts, but yes, perhaps. – argarevarg Dec 21 '14 at 0:28
  • @Virusboy: I think it's a valid answer that hasn't occurred here yet. – David Foerster Dec 21 '14 at 0:29

You could also use Rhythmbox, banshee, or a similar software. They can scan your music directories.

You can choose to preserve the directory or (in your case) the software can move the files in to a rhythmbox/banshee directory which would eliminate the hierarchy like you wanted.


cp /target/directory/* /desination/directory/ -r

Then, delete the original if it was successful.

(cd ~/Music ; tar cf - ) | (cd /path/to/other/directory; tar xf -) 
  • That's copy, not move. – 0xC0000022L Feb 29 '12 at 18:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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