jk6411666109
jk6411666109

Reputation: 21

KSH script to move files to SFTP folder

I'm attempting to move a very specifically named file from one folder to another (which ends up being SFTP'd to another server).

The file name should look like: reports_aug_30.csv

Using my present script, I'm having issues getting it in to that format. I'm getting: aug_30_reports.csv

What I'd like to do is move the date at the beginning and then have it keep the name for the rest of the filename.

Does anyone have any suggestions on how to format the report name as mentioned?

The two related functions in my script are:

function REPORTS
{
        # specify SFTP report path
        SFTPDIR=/home/josh/domain/sftp/reports
        [email protected]
        CHKSFTP
        typeset -l FILEDR=$tmp
        typeset -l FILENM=reports.csv
        typeset -l MIXFNM=$FILEDR/$FILENM
        typeset -l NEWFILENM
        if [[ -f $MIXFNM ]]
        then
                ls -al $MIXFNM | awk '{print $6,$7}' | while read MONTH DAY
                do
                        NEWFILENM=$FILENM_${MONTH}_${DAY}
                        echo "---------------------------------------------"
                        echo "[ `date` ]"
                        echo "** $FNCTN file located on `hostname` ! **"
                        echo "moving: $MIXFNM to $SFTPDIR/$NEWFILENM"
                        mv $MIXFNM $SFTPDIR/$NEWFILENM
                done
        else
                echo "---------------------------------------------"
               # echo -e "[ `date` ] - No files with a name of $FILENM located in $FILEDR were found on [ `hostname` ] - Please check the respective operations for failures" | mailx -s "$FNCTN File not found [ `hostname` ]!" $ALERT_LIST
                echo "[ `date` ]"
                echo -e "WARNING: No files with a name of $FILENM located in $FILEDR were found on [ `hostname` ]! \n Email alert sent to $ALERT_LIST"
        fi
}

function CHKSFTP
{
## check for the SFTP directory before we continue, if it fails then we exit with failure
if [[ ! -d $SFTPDIR ]]
then
        echo "Please Mount the CIFS share /home/josh/domain/sftp/reports" | mailx -s "SFTP Share Not Mounted [ `hostname` ]!" $ALERT_LIST
        exit 1
fi
}

Upvotes: 2

Views: 225

Answers (1)

CharlieTango92
CharlieTango92

Reputation: 169

So you probably want something like this in your script:

FILENAME=$reports_(date +"%b_%d").csv

so if we echo this variable it shows:

echo $FILENAME

reports_Aug_30.csv

using your variable $FILENAME (you'd probably set your path elsewhere to be cleaner)

just do mv $OLDPATH/$FILENAME $NEWPATH/$FILENAME

with a little syntax cleanup.

Upvotes: 1

Related Questions