cp: argument list too long
Oct. 18th, 2004 05:27 pmEver get the error mv: argument list too long or cp: argument list too long in Linux? Yeah, I know I shouldn't work with directories that have thousands of files in them, but sometimes that's not an option. Anyway, I did a little research and found this interesting workaround:
(cd $SOURCE && tar cfv - .) | (cd $TARGET && tar xfv -)
This command will change to your source directory, start tarring the contents, and write the tarball to stdout. The command on the right side of the pipe will change to your target directory and extract the tarball from stdin.
The reason for changing into the appropriate directories is because tar preserves the directory structure of whatever it archives, so you don't want it to include any parent directories of $SOURCE. The && characters mean that the command on the right side of them is only executed if the cd command on the left side of them executed successfully. Note that the above command requires you have enough disk space for a copy of the files to be made.
I love UNIX shell scripting. :-)
(cd $SOURCE && tar cfv - .) | (cd $TARGET && tar xfv -)
This command will change to your source directory, start tarring the contents, and write the tarball to stdout. The command on the right side of the pipe will change to your target directory and extract the tarball from stdin.
The reason for changing into the appropriate directories is because tar preserves the directory structure of whatever it archives, so you don't want it to include any parent directories of $SOURCE. The && characters mean that the command on the right side of them is only executed if the cd command on the left side of them executed successfully. Note that the above command requires you have enough disk space for a copy of the files to be made.
I love UNIX shell scripting. :-)
(no subject)
Date: 2004-10-18 09:57 pm (UTC)(no subject)
Date: 2004-10-18 09:58 pm (UTC)Thanks for reminding me. :)
(no subject)
Date: 2004-10-18 10:02 pm (UTC)(no subject)
Date: 2004-10-18 10:22 pm (UTC)(no subject)
Date: 2004-10-18 10:26 pm (UTC)find . -type f -exec mv {} $TARGET \;
(no subject)
Date: 2004-10-18 10:28 pm (UTC)(no subject)
Date: 2004-10-18 10:36 pm (UTC)(no subject)
Date: 2004-10-18 10:36 pm (UTC)(no subject)
Date: 2004-10-18 11:15 pm (UTC)Anyhow... xargs, arcane, ludicrously powerful. Like so many other UNIX tools. ;)
(no subject)
Date: 2004-10-18 11:17 pm (UTC)something like "find $SOURCE -print | cpio -pdvum $TARGET" I believe is the format, but it's been few years since I have used cpio that way.
(no subject)
Date: 2004-10-18 11:37 pm (UTC)Also, rsync, while a bit more cpu overhead, especially for large file lists, is a nifty way to copy a bunch of files from one directory to another. It's especially slick if for some reason you have to interrupt the copy and resume later. "rsync -av $SOURCE $TARGET" (and add other flags if you want to delete files in $TARGET that are not in $SOURCE).
find/mv, as has been said, is also an option. I think that caching essentially eliminates the overhead of reloading mv each time.
(no subject)
Date: 2004-10-19 01:01 am (UTC)(no subject)
Date: 2004-10-19 01:03 am (UTC)However.... not with cp or mv; both can handle entire directories without having to have an argument for each file.
I usually deal with that using foreach; which is odd as foreach takes parameters anyway, but it'd seem to support more parameters, probably because it's not an actual command
like instead of mv * somewhere
foreach i in * ; do mv $i somewhere ; done
Yes, there is overhead for running mv with each file, and it /is/ noticeable
(no subject)
Date: 2004-10-19 01:08 am (UTC)(no subject)
Date: 2004-10-19 01:11 am (UTC)(no subject)
Date: 2004-10-19 04:13 am (UTC)(no subject)
Date: 2004-10-19 05:34 am (UTC)(no subject)
Date: 2004-10-19 05:35 am (UTC)(no subject)
Date: 2004-10-19 11:53 am (UTC)(no subject)
Date: 2004-10-19 02:18 pm (UTC)Of course if you wanted to be sneak you could rsync and then suppress the various files you don't want to copy. The nice thing about using rsync as a local large copy is its resumable....
(no subject)
Date: 2004-10-19 02:26 pm (UTC)(no subject)
Date: 2004-10-19 02:40 pm (UTC)cd $SOURCE; cp -r ./ $TARGET
(no subject)
Date: 2004-10-19 07:26 pm (UTC)I was working on a SCO Unix box (which might still be running) and somehow a directory was created that contained a copy of everything one directory above it, and then proceeded to, very likely, infinitely recurse. I was never able to find the "bottom" of it and it didn't seem to have any sort of performance impact so I just left it alone.