Hello Folks,
How are you all?
I have written below script to get the list of files from ftp.
Code:
#!/bin/bash
#set -x
#get a list of files and dates from ftp and remove files older than n days
ftpserver="pns95a-4402.corpny.csfb.com"
ftpuser="adoshi9"
ftppass=cs@Cogni2016
putdir="/app/opera/operaqa7/TgtFiles/PAC/test"
ndays=15
#work out cutoff dates.
MM=`date --date="$ndays days ago" +%b`
DD=`date --date="$ndays days ago" +%d`
TT=`date --date="$ndays days ago" +%s`
echo removing files older then $MM $DD from $ftpsite directory $putdir
#get dirlist from ftp
listing=`ftp -i $ftpserver << EOF
user $ftpuser $ftppass
binary
cd $putdir
ls -l
quit
EOF
`
filelist=( $(printf "( $listing\n )" | tail -n +5 |head -n -1 | awk '{print $9'} ) )
echo "${filelist[@]}"
echo "${#filelist[@]}"
actuallist=( $(printf "( $listing\n )" | tail -n +5 |head -n -1 ) )
for (( FNO=0; FNO < ${#filelist[@]}; FNO++ ));do
fdate="${actuallist[`expr $FNO + 5`]} ${actuallist[`expr $FNO + 6`]} ${actuallist[`expr $FNO + 8`]}"
echo "$fdate";
done
Basically I am trying to get the list of files from ftp and compare it with current date timestamp. If it is older than 15 days it should get deleted with this script.
The issue is that in above code ouput of
echo "${#filelist[@]}" is 6 and output of
echo "${actuallist[@]}" is as below.
-rw-rw-rw- 1 addy test 2540 Sep 15 05:47 abc-20150630-20150630.gz
-rw-rw-rw- 1 addy test 126 Sep 15 05:47 abc-20150630-20150630.gz.tmp.ftp.dtl
-rw-rw-rw- 1 addy test 27676 Sep 11 2015 PAC-20150630-20150630.gz
-rw-rw-rw- 1 addy test 126 Sep 14 16:32 PAC-20150630-20150630.gz.tmp.ftp.dtl
-rw-rw-rw- 1 addy test 8890 Sep 15 04:22 AMS-20150630-20150630.gz
-rw-rw-rw- 1 addy test 126 Sep 15 04:22 AMS-20150630-20150630.gz.tmp.ftp.dtl
When script executes for loop it space as field seperater and consider the string as pne argument.
So when value of FN=0 then ouput of echo "$fdate"(which seems correct) is
Sep 15 abc-20150630-20150630.gz
But when value of FN increases in next iteration FN=1 and ouput of echo "$fdate" is
15 05:47 -rw-rw-rw- which is not supposed to be. Here I should get
Sep 15 abc-20150630-20150630.gz.tmp.ftp.dtl
what can be done here to achive above output?