cp: Argument list too long

Michael Wittman plug-discuss@lists.plug.phoenix.az.us
Mon, 9 Dec 2002 22:53:20 -0700


On Mon, Dec 09, 2002 at 02:15:19PM -0700, Bill Earl wrote:
> Is anyone aware of a separate file copying or moving (I need to do
> both, depending on the situation) utility that can handle very large
> numbers of files?  Would switching to a different shell solve this
> (if so, which one?), or is it a limit on the cp and mv commands?

It's a limitation of the shell.  The fastest and easiest way to do
this is probably to create a executable script file (say "cp-files")
like the following:

#!/bin/bash
cp "$@" /newdir

Then run the command:
find /mydir/myfiles -name \*.pdf -print0 | xargs -0 -n 2500 -s 10000 cp-files

xargs reads arguments from stdin, repeatedly puts as many as will fit
on the command line, then calls the command given.  The -n and -s may
be necessary because the cp command invocation in the script will have
one more argument and a little more text than the cp-files invocation.
Find piped to xargs will be much faster than invoking cp on each file
individually.

Normally you don't need to create a separate script to use with xargs, but
cp (and mv) have the peculiar property that the last argument is special.

-Mike