How can I move many files without having Argument list too long?

Awah Hammad picture Awah Hammad · Jan 9, 2016 · Viewed 19.4k times · Source

I am trying to move about 700,000 .jpg files from one directory to another in my Ubuntu server. I tried the following:

xargs mv *  -t /var/www/html/

and

echo (*.jpg|*.png|*.bmp) | xargs mv -t /var/www/html/

and

echo (*.jpg) | xargs mv -t /var/www/html/

and

find . -name "*.jpg" -print0 | xargs mv * ../

and they all give me the same error: /usr/bin/xargs: Argument list too long

what should I do? Please help me out. Thanks :)

Answer

Jeffrey Descan picture Jeffrey Descan · Jan 9, 2016

If you use find I would recommend you to use the -exec attribute. So your result should be find . -name "*.jpg" -exec mv {} /home/new/location \;.

However I would recommend to check what the find command returns you, replacing the exec part with: -exec ls -lrt {} \;