1748128 Members
3630 Online
108758 Solutions
New Discussion юеВ

Re: Arguments too long

 
LawrenceLow
Advisor

Arguments too long

Hi there,

Apparently I keep recieve an error message when I try to copy some files to other directory.

/% cp /export1/uw/ECM/images/BGIR2093*.txt /export1/uw/ECM/backup/images
Arguments too long.


Please assist.
3 REPLIES 3
Laurent Menase
Honored Contributor

Re: Arguments too long

for i in /export1/uw/ECM/images/BGIR2093*.txt
do
cp $i /export1/uw/ECM/backup/images
done



Or
ls /export1/uw/ECM/images/BGIR2093*.txt | xargs -i -t cp {} /export1/uw/ECM/backup/images
Matti_Kurkela
Honored Contributor

Re: Arguments too long

> for i in /export1/uw/ECM/images/BGIR2093*.txt

This "for" command line has the same risk of being too long after wildcard expansion as the original cp command.

The trick is to use "find" and pass any necessary wildcards to it in quotes, so they won't be expanded by the shell.

But the "find" command looks into sub-directories too, so you should verify it does what you want before actually making it do anything that's difficult to undo:

find /export1/uw/ECM/images -name 'BGIR2093*.txt' -exec echo cp {} /export1/uw/ECM/backup/images/ \+

This should display a list of (probably very long) "cp" commands. If the commands look OK, just remove the "echo" in the middle and the cp commands will be actually executed, instead of displayed.

Limiting "find" to a single directory in a portable way may be tricky:
http://www.in-ulm.de/~mascheck/various/find/

MK
MK
Dennis Handly
Acclaimed Contributor

Re: Arguments too long

>Matti: -exec echo cp {} /export1/uw/ECM/backup/images/ \+

Unfortunately you can't use {} with + except last. You'll need to create a script that reverses the first parm with the last:
find ... -exec cp_reverse /export1/uw/ECM/backup/images/ {} +
#!/usr/bin/sh
target=$1
shift
cp "$@" "$target"

>Laurent: ls /export1/uw/ECM/images/BGIR2093*.txt | xargs

This will also fail. You need to add add grep to the pipeline and you would have to cd to that directory:
cd /export1/uw/ECM/images
ls | grep "\.txt$" | xargs ...

Also if you cd to the directory, you may be able to use:
cp BGIR2093*.txt /export1/uw/ECM/backup/images