Command Line – Move Files by Type Recursively to Another Directory

command linedirectoryfindrename

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'

Best Answer

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

Related Question