Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

3 Answers 3

up vote 8 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).

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

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

share|improve this answer
1  
please don't forget to take backup before trying above commands :-). –  Hemant Sep 3 '10 at 21:06
2  
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
2  
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
2  
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
2  
why, ehm, sudo? –  maxschlepzig Sep 3 '10 at 21:07
1  
I swear I have no idea why I included that there :( –  Coding District Sep 3 '10 at 21:20
1  
+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

 
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.