35

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'
28

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 \;

| 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 'SO- stop being evil' Sep 3 '10 at 21:41
  • 2
    with mv from gnu core utils you can use mv -t <targetdir> and + instead of \; – user601 Sep 3 '10 at 22:29
13
find /thisdir -type f -name "*.ogg" -exec mv {} /somedir \;

You kinda interchanged the arguments for mv

| improve this answer | |
12

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
| improve this answer | |
  • beyond convenient in bash – Mark Jan 11 '18 at 12:26

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.