2009/3/4

Argument list too long when copying/deleting/moving files on Linux

If you do a lot of command line stuff in Linux, sooner or later you'll come across the "argument list too long" error when copying, moving, deleting, listing etc files in a directory that contains a lot of files. This post looks at a couple of ways of solving this problem.
will result in errors like this:
-bash: /bin/mv: Argument list too long
-bash: /bin/cp: Argument list too long
-bash: /bin/rm: Argument list too long
-bash: /bin/ls: Argument list too long

To workaround the problem you can use xargs or find, or a combination of the two.

Using the "ls" command, you could do this to move all the files from source into target:
ls -1 ./*.dat | xargs -i mv {} /home/user/targe/

Using find to do the same move from source to target, you could do this:

find . -name "*.txt" -maxdepth 1 -exec mv {} /home/user/targe/ \;
or this, using xargs instead of the -exec flag:
find . -name "*.txt" | xargs -i mv {} /home/user/targe/
Reference: http://www.electrictoolbox.com/argument-list-too-long-linux/