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'

4 Answers 4


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

  • 1
    please don't forget to take backup before trying above commands :-).
    – Hemant
    Sep 3, 2010 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. Sep 3, 2010 at 21:10
  • @maxschlepzig: good point. I will edit.
    – Hemant
    Sep 3, 2010 at 21:13
  • 2
    I strongly suggest using mv -i here, so you don't risk overwriting files if something unexpected happens. Sep 3, 2010 at 21:41
  • 2
    with mv from gnu core utils you can use mv -t <targetdir> and + instead of \;
    – user601
    Sep 3, 2010 at 22:29

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
  • beyond convenient in bash
    – Mark
    Jan 11, 2018 at 12:26
find /thisdir -type f -name "*.ogg" -exec mv {} /somedir \;

You kinda interchanged the arguments for mv

  • 3
    WARNING!!! This does not keep the recursive folder structure at the destination!
    – James
    Jul 21, 2020 at 10:21

i got "directory not empty" error. to fix this i run:

find . -name 'node_modules' -type d -prune -exec mkdir -p ./another/dir/{} \; -exec mv -i {} ./NODE_MODULES/{} \;

this will keep the directory structure.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .