ftp most recently modified file

Hi what is the most optimum way to ftp the most recently modified file starting with a particular string.

i tried this

    ftp -n 2>logfile 1>&2 <<EOF
    open xxxxxx
    user xxxx xxxx
    prompt
    ls -ltr f* res
    !var=`tail -1 |awk { print $9 }'`
    bye
EOF

that gives the error in ls any suggestions or alternatives???

Here is a script that demonstrates how to use ftp to do a "dir" and parse the results. I wanted to keep it as simple as possible, so you might need to adjust the read statement.

#! /usr/bin/ksh

#
# Connnect to the host, execute a "dir" sending the output to a local
# file called "listing".

HOST=this
USER=that
PASSWD=secret

exec 4>&1
ftp -nv >&4 2>&4 |&

print -p open $HOST
print -p user $USER $PASSWD
print -p prompt
print -p dir listing
print -p bye


#
#  Get ready to decode the directory listing

typeset -Z2 nmonth day
typeset -i8 octal

set +A ts $(date "+%Y %m")  THISYEAR THISMONTH
THISYEAR=${ts[0]}
THISMONTH=${ts[1]}
((LASTYEAR=THISYEAR-1))

#
#  Function to convert month to numeric

conv_month() {
        typeset -l month
        month=$1
        case $month in
        jan)    nmonth=1  ;;
        feb)    nmonth=2  ;;
        mar)    nmonth=3  ;;
        apr)    nmonth=4  ;;
        may)    nmonth=5  ;;
        jun)    nmonth=6  ;;
        jul)    nmonth=7  ;;
        aug)    nmonth=8  ;;
        sep)    nmonth=9  ;;
        oct)    nmonth=10 ;;
        nov)    nmonth=11 ;;
        dec)    nmonth=12 ;;
        *)      nmonth=0  ;;
        esac
        echo $nmonth
        return $((!nmonth))
}



exec < listing


#
#  Read Loop
#  You need to adjust the number of "junk" entries in the read statement to line
#  up the fields.  There might be two formats:
#   -rwxr--r--   1 users        959 Dec 18  2001 listr.old
#   -rwxr--r--  1 user  None  2215 Feb 26 23:33 ftpjob


while IFS=" " read permstring junk junk size month day swing rawname ; do

        #
        # Get rid of total line and any entries for directories, symlinks, etc.

        char1=${permstring%%${permstring#?}}
        if [[ $char1 != "-" ]] ; then
                continue
        fi

        #
        #  decode permissions
        set -A perms -- $(print -- ${permstring#?} | sed 's/./& /g')
        extras=0
        [[ ${perms[2]} = S ]] && { ((extras=extras+4000)); perms[2]=- ; }
        [[ ${perms[2]} = s ]] && { ((extras=extras+4000)); perms[2]=x ; }
        [[ ${perms[5]} = S ]] && { ((extras=extras+2000)); perms[5]=- ; }
        [[ ${perms[5]} = s ]] && { ((extras=extras+2000)); perms[5]=x ; }
        [[ ${perms[8]} = T ]] && { ((extras=extras+1000)); perms[8]=- ; }
        [[ ${perms[8]} = t ]] && { ((extras=extras+1000)); perms[8]=x ; }

        binary=2#$(print -- ${perms[@]} | sed 's/ //g;s/-/0/g;s/[^0]/1/g')
        ((octal=binary))
        result=$(echo $octal)
        result=${result#??}
        ((result=result+extras))

        #
        # Decode date and time and convert it to yyyymmddhhmm
        # If no time is present, use 0000.
        # If no year is present, figure it out.
        nmonth=$(conv_month $month)
        if [[ $swing = *:* ]] ; then
                if [[ $nmonth > $THISMONTH ]] ; then
                        ((year=LASTYEAR))
                else
                        ((year=THISYEAR))
                time1=${swing%???}
                time2=${swing#???}
                time="${time1}${time2}"
                fi
        else
                year=$swing
                time="0000"
        fi

        echo $name size=$size perms=$result "timestamp=["$year $nmonth $day ${time}"]"
done


exit

here is what i could find the best. Please suggest me better ways than this:

for filename in `cat filelist` ; do
export file=`rexec remote_server ls -t ${filename}* | head -1`
echo "THE FILENAME IS" $filename
ftp -n <<-EOF
open bdwux001
user xxxxx xxxxxxx
mget $file
bye
EOF
echo "END"
done

You seem to reject my technique without commenting on it. If your script is working I guess it's good enough. But you are depending on the rexec protocol being enabled and often it is not available. You also use one ftp process per file. If you switch to a ksh coprocess, a single ftp process is enough for the entire job.

I AM SORRY, IF I HAVE SHOWED ANY SIGNS OF REJECTION. the only reason why I went in for this process is that my team wasnt comfortable with that. I would never doubt your techniques, you do splendid job always. and rexec was enabled and i .netrc is required before running it.

I am not aware of coprocess could you please explain it?? THANKS FOR ALL YOUR HELP AND EFFORTS TO HELP ME AND ALL OTHERS IN THE FORUM.

A coprocess is setup by:
command |&

Then "print -p" sends stuff to the coprocess as standard input and "read -p" gets stuff back from the coprocess' standard output. This only works with ksh and it is documented on the ksh man page.

Thats a great way to start off with. Thanks for sharing your wisdom!!!!! :slight_smile: