I am trying to move uploaded file to another server using scp. following is the script i am using and error;

"mv.sh"

#!/bin/sh
now=$(date +"%y-%m-%d-%M")
    cd /var/www/upload

        if [ -f /var/www/upload/* ];
        then
                    scp -P 88 * user@192.168.2.1:backup/
        STATUS=$?
        echo "$now-S1:succesful." >> /var/log/mv_to.log
        else
            echo "$now-S1:Error!!" >> /var/log/mv_to.log
    fi

error;

[: 13: /var/www/upload/1.doc: unexpected operator

If there is only one file it works fine and if there are two or more files it says unexpected operator. Please correct me here.

link|improve this question

Isn't rsync a better option than scp? Your script wants to copy every file in the directory. If you don't remove files from upload after they've been copied, you'll re-copy them when you run your script next. Also, rysnc comes with a log file. – Chris Ting May 25 '11 at 15:27
feedback

1 Answer

up vote 1 down vote accepted

The problem is with your if statement. You can replace it with:

    if [ -d /var/www/upload/ ];

(instead of checking that a file exists, which gives an error if there's more than one file, it checks that the directory exists)

link|improve this answer
But I also need to log if file doesn't exist?? – Himalay May 25 '11 at 15:12
Unless he plans on creating and deleting the directory over and over, isn't it better that he not include the if statement? – Chris Ting May 25 '11 at 15:19
To check that at least some files exist in the upload directory, use: if [ "$(ls -A /var/www/upload/)" ]; – timginn May 25 '11 at 15:25
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.