ftp most recently modified file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ftp most recently modified file
# 1  
Old 02-26-2007
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

Code:
    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???
# 2  
Old 02-27-2007
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.

Code:
#! /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

# 3  
Old 02-27-2007
here is what i could find the best. Please suggest me better ways than this:

Code:
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


Last edited by ahmedwaseem2000; 02-27-2007 at 11:05 AM..
# 4  
Old 02-27-2007
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.
# 5  
Old 02-27-2007
Quote:
Originally Posted by Perderabo
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.
# 6  
Old 02-27-2007
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.
# 7  
Old 02-27-2007
Quote:
Originally Posted by Perderabo
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!!!!! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Copy files from one drive to another, keeping most recently modified files

Hi all, I am a bit of a beginner with shell scripting.. What I want to do is merge two drives, for example moving all data from X to Y. If a file in X doesn't exist in Y, it will be moved there. If a file in X also exists in Y, the most recently modified file will be moved to (or kept) in... (5 Replies)
Discussion started by: apocolapse
5 Replies

2. Shell Programming and Scripting

Script to FTP a modified file

Hello, I am fairly new to shell scripting. I see a lot of examples out there of how to find if a file has been modified within a certain period of time. What I'm looking for help with is a script that will run and I'm thinking check for the last 24 hours but if not just check at runtime to see... (6 Replies)
Discussion started by: PyroPlasm
6 Replies

3. Shell Programming and Scripting

Search for a recently updated file

Hi, I am looking for a command to search for a specific file which was recently modified in the current directory leaving some unwanted files to be listed. For example, when I try ls - lrt it shows the following output. I want to ommit the files with the name 'resend' and... (3 Replies)
Discussion started by: svajhala
3 Replies

4. UNIX for Advanced & Expert Users

command for recently modified files - "find" command not working

I have three files a.txt , b.txt , c.txt in a directory called my_dir1 .These files were created before two or three months . I have a tar file called my_tar1.tar which contains three files a.txt , b.txt , d.txt . Somebody untarred the my_tar1.tar into my_dir1 directory. So existing two files were... (1 Reply)
Discussion started by: joe.mani
1 Replies

5. Shell Programming and Scripting

FTP files modified after a particular date between servers

Hi all, i need to write a shell script to transfer a file modified after a particular date from one server to another. I searched for the related posts in this forum and got hints and snippets for it. i tried the below code ftp serverA user uname pwd lcd to_dir cd from_dir files=$(find... (7 Replies)
Discussion started by: mick_000
7 Replies

6. Shell Programming and Scripting

Find user owner of the most recently file in the system

Good evening everybody, I have to find the user owner of the most recently file in the system How can I do? :confused: (5 Replies)
Discussion started by: Guccio
5 Replies

7. UNIX for Dummies Questions & Answers

Find most recently modified directories

How do I do it? Simple answers preferred... using BASH.. the less code the better. I want to find out where Indesign is caching PDF tmp data ... I figure this is a good way to do it.. either way i wanna know how to do it. (2 Replies)
Discussion started by: glev2005
2 Replies

8. Shell Programming and Scripting

find recently modified/ updated file

hi gurus, i would like to know how can i find logs files which were recently modified or updated? :confused: using this command? find . -name "*.log" -mtime ?? so what should i put for mtime? thanks. wee (9 Replies)
Discussion started by: lweegp
9 Replies

9. UNIX for Dummies Questions & Answers

how to retrieve original contents of a modified file (modified using vi)

Made changes to a file using vi editor and saved those changes now realised that the changes are not required How can I get the previous version of the file.i.e the one which was there on which I had made changes (3 Replies)
Discussion started by: novice100
3 Replies

10. UNIX for Dummies Questions & Answers

chmod command for recently modified files

hello! is there a way for me to use the chmod command to change permissions for several files all at once -based on the fact that these files were all most recently modified TODAY ? I can't use a wildcard on their filenames because the filenames are varied. But I was hoping I could somehow do... (2 Replies)
Discussion started by: polka_friend
2 Replies
Login or Register to Ask a Question