14

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 :)

Bobrovsky
  • 13,789
  • 19
  • 80
  • 130
Awah Hammad
  • 165
  • 1
  • 1
  • 9
  • Related: [Does "argument list too long" restriction apply to shell builtins?](https://stackoverflow.com/questions/47443380/does-argument-list-too-long-restriction-apply-to-shell-builtins) – codeforester Nov 22 '17 at 21:18

3 Answers3

26

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

Jeffrey Descan
  • 291
  • 4
  • 5
6

Try:

find /path/to/old-directory -type f | xargs -i mv "{}" /path/to/new-directory
Ashwin
  • 61
  • 1
  • 1
2

You could have tried:

 for f in *.jpg do;
   mv -tv $f /var/www/html/
 done
 for f in *.png do;
   mv -tv $f /var/www/html/
 done
 for f in *.bmp do;
   mv -tv $f /var/www/html/
 done

also, you should carefully read xargs(1); I strongly suspect that

 find . -name "*.jpg" -print0 | xargs -n 1000 -I '{}' mv '{}'  ../

should work for you

At last, learn more about rename(1). It is probably enough for the job.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • On raspberry pi I have played with this command and it started working when semicolon was moved before 'do' keyword so command looked like this: "for f in *.jpg; do mv -f $f /var/www/html/jpgs/; done" – Wojciech Jakubas Oct 21 '16 at 23:49