Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

error on script to copy files

SOLVED
Go to solution
NDO
Super Advisor

error on script to copy files

Hi

I have the following script to copy files from one directory to another:

 

#!/bin/sh
touch -mt 201304240000 /var/tmp/ref1
touch -mt 201305152359 /var/tmp/ref2
find /moneta_collected02/in_psl -type f \( -newer /var/tmp/ref1 -a ! -newer /var/tmp/ref2 \) > file_lst
cp -pr $(< file_lst) /moneta_collected02/in_psl1/

 but when I run I have the following error:

 

root@moneta # ./transf
find: stat() error /moneta_collected02/in_psl/PSLCCN4-130106-0731-90574.gz: No such file or directory
./transf: syntax error at line 5: `(' unexpected

 please can you help?

14 REPLIES
Dennis Handly
Acclaimed Contributor

Re: error on script to copy files

>find: stat() error /moneta_collected02/in_psl/PSLCCN4-130106-0731-90574.gz: No such file or directory

 

It's almost as if the file as removed just as it found it.  ??

You can suppress this by adding "2> /dev/null" to the end of the find.

 

>./transf: syntax error at line 5: `(' unexpected

 

You may have to check to see if file_lst is not empty before doing the cp:

if [ -s file_lst ]; then

   cp -pr $(< file_lst) /moneta_collected02/in_psl1/

fi

NDO
Super Advisor

Re: error on script to copy files

 

Hi!

 

With the change suggested by Dennis, I am having the same error:

 

root@moneta # ./transf
find: stat() error /moneta_collected02/in_psl/PSLCCN4-130106-0731-90574.gz: No such file or directory
./transf: syntax error at line 7: `(' unexpected

 line 7 is :

 

 cp -pr $(< file_lst) /moneta_collected02/in_psl1/

 

Dennis Handly
Acclaimed Contributor

Re: error on script to copy files

Have you looked inside of file_lst to see if valid filenames?

 

Otherwise that syntax is perfectly valid for a real shell.

NDO
Super Advisor

Re: error on script to copy files

this is what is inside:

 

root@moneta # head file_lst
/moneta_collected02/in_psl/130304-1530-PSLCCN2-130506-0009-198970.gz
/moneta_collected02/in_psl/130304-1530-PSLCCN2-130506-0009-198969.gz
/moneta_collected02/in_psl/130304-1530-PSLCCN2-130506-2332-203175.gz
/moneta_collected02/in_psl/130304-1530-PSLCCN2-130506-2332-203176.gz
/moneta_collected02/in_psl/130304-1530-PSLCCN2-130502-2331-185808.gz
/moneta_collected02/in_psl/130304-1530-PSLCCN2-130502-2330-185806.gz
/moneta_collected02/in_psl/130304-1530-PSLCCN2-130502-2331-185807.gz
/moneta_collected02/in_psl/130304-1530-PSLCCN2-130502-2357-185888.gz
/moneta_collected02/in_psl/130304-1530-PSLCCN2-130502-2357-185887.gz
/moneta_collected02/in_psl/130304-1530-PSLCCN2-130502-2357-185886.gz
root@moneta #

 which is correct, unless the script does not want the directory  (moneta_collected02) and the subdirectory (in_psl1)

Dennis Handly
Acclaimed Contributor

Re: error on script to copy files

>this is what is inside: ... unless the script does not want the directory

 

No, that full path is fine.

What OS version are you using?

NDO
Super Advisor

Re: error on script to copy files

Hi

 

It does work now after inserting the condional format (if) on HP-UX 11.31, but I try to use a similar script on a solaris 10 system and it gives me that error message.

Dennis Handly
Acclaimed Contributor

Re: error on script to copy files

>I try to use a similar script on a solaris 10 system and it gives me that error message.

 

Because Solaris isn't using a real shell for /bin/sh.  You need to use a Posix or ksh.

Or you can go back to the bourne dark ages:

   cp -pr `cat file_lst` ...

NDO
Super Advisor

Re: error on script to copy files

you mean changing the #!/bin/sh by #!/bin/ksh ?
NDO
Super Advisor

Re: error on script to copy files

After changing to ksh now I have a different error:

 

./transf[7]: /usr/bin/cp: arg list too long

 But I believe if the shorten the markers :

 

touch -mt 201304240000 /var/tmp/ref1
touch -mt 201305152359 /var/tmp/ref2

 I can solve this

Dennis Handly
Acclaimed Contributor

Re: error on script to copy files

>you mean changing the #!/bin/sh by #!/bin/ksh?

 

Yes, that will do it.

(Of course there may be issues similar to HP-UX where shell scripts may have to use /sbin/sh so they work if /usr isn't mounted.)

NDO
Super Advisor

Re: error on script to copy files

After changing to ksh now I have a different error:

./transf[7]: /usr/bin/cp: arg list too long
But I believe if the shorten the markers :

touch -mt 201304240000 /var/tmp/ref1
touch -mt 201305152359 /var/tmp/ref2
I can solve this
Dennis Handly
Acclaimed Contributor
Solution

Re: error on script to copy files

>./transf[7]: /usr/bin/cp: arg list too long
>But I believe if the shorten the markers :

 

But why would you want to settle?  :-)

 

The correct solution on HP-UX and Linux would be to use: find ... -exec special-cp-script {} +

But assuming your files are large and it isn't worth trying to use xargs or write that above script, can use:

for file in $(< file_lst); do

   cp -pr $file /moneta_collected02/in_psl1/

done

NDO
Super Advisor

Re: error on script to copy files

thanks a lot, I have tried that but I am certain is going to work, thanks again for your help
Highlighted
Dennis Handly
Acclaimed Contributor

Re: error on script to copy files

>I have tried that but I am certain is going to work, thanks again for your help

 

Sure.

If you have performance issues, we can use the find/xargs solutions to invoke cp with multiple files.