Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What would be a good way to move a file type from a directory and all of its sub-directories?

Like "move all *.ogg in /thisdir recursively to /somedir". I tried a couple of things; my best effort was (still not that great):

find /thisdir -type f -name '*.ogg' -exec mv /somedir {} \;

It returned on each line before each file name,

mv: cannot overwrite non-directory `/thisdir/*.ogg' with directory `/somedir'
share|improve this question
up vote 14 down vote accepted

you can use find with xargs for this

find /thisdir -type f -name "*.ogg" -print0 | xargs -0 -Imysongs mv -i mysongs /somedir

The -I in the above command tells xargs what replacement string you want to use (otherwise it adds the arguments to the end of the command).

In your command just try to move '{}' after mv command.

find /thisdir -type f -name '*.ogg' -exec mv -i {} /somedir \;

share|improve this answer
please don't forget to take backup before trying above commands :-). – Hemant Sep 3 '10 at 21:06
Btw, for the first -print0 for find and -0 for xargs should be used as possible, to avoid whitespace in filename problems. – maxschlepzig Sep 3 '10 at 21:10
@maxschlepzig: good point. I will edit. – Hemant Sep 3 '10 at 21:13
I strongly suggest using mv -i here, so you don't risk overwriting files if something unexpected happens. – Gilles Sep 3 '10 at 21:41
with mv from gnu core utils you can use mv -t <targetdir> and + instead of \; – hop Sep 3 '10 at 22:29
find /thisdir -type f -name "*.ogg" -exec mv {} /somedir \;

You kinda interchanged the arguments for mv

share|improve this answer
why, ehm, sudo? – maxschlepzig Sep 3 '10 at 21:07
I swear I have no idea why I included that there :( – Coding District Sep 3 '10 at 21:20
+1 for honesty :-) – Hemant Sep 3 '10 at 21:28

In zsh or bash 4, to gather all *.ogg files into /somedir:

mv /thisdir/**/*.ogg /somedir

If you wanted to reproduce the directory hierarchy: (warning, typed directly into the browser)

rsync -a --prune-empty-dirs --include='*/' --include='*.ogg' --exclude='*' /thisdir /somedir
share|improve this answer

Your Answer


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.