How can I move files by type recursively from a directory and its sub-directories to another directory?

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'

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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 ;

Method 2

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

Method 3

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

You kinda interchanged the arguments for mv

Method 4

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x