System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

mv command and sub directories

Mike Loring
Occasional Visitor

mv command and sub directories

I want to move all files ending in .jpg on the root dirve to a directory called photos. Can I use mv -r /root *.jpg ~photos or something like that which is recursive and looks for .jpg files in all sub directores of the volume?
3 REPLIES
Dennis Handly
Acclaimed Contributor

Re: mv command and sub directories

find / -name "*.jpg" -exec mv {} ~/photos \;

(Unfortunately you can't use +, unless you write a script to do the mv and reverse the args.)
Ganesan R
Honored Contributor

Re: mv command and sub directories

Hi Mike,

In addition to Dennis reply,

If you want to search and move only from root filesystem and do not cross other mount points use this,

#find / -xdev -name "*.jpg" -exec mv {} ~/photos \;


Question for Dennis,

#find / -xdev -name "*.jpg" -exec mv {} ~/photos +

won't this work? unfortunately not near to the system to test it..
Best wishes,

Ganesh.
Dennis Handly
Acclaimed Contributor

Re: mv command and sub directories

>Ganesan: find / -xdev -name "*.jpg" -exec mv {} ~/photos +
>won't this work?

I tried those before replying. Neither find + nor xargs works with multiple arguments inserted other than the end.
In fact find(1) says everything from the "{}" to the "+" is replaced.

So you are left with writing a script, if you want performance:
find / -xdev -name "*.jpg" -exec move_them ~/photos {} +
move_them:
#!/usr/bin/ksh
TARGET=$1
shift
mv "$@" "$TARGET"