Sponsored Content
Top Forums Shell Programming and Scripting ftp most recently modified file Post 302108491 by Perderabo on Tuesday 27th of February 2007 12:16:59 AM
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

 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
pam_console_appy(8)					   System Administrator's Manual				       pam_console_appy(8)

NAME
pam_console_apply - set or revoke permissions for users at the system console SYNOPSIS
pam_console_apply [-r] DESCRIPTION
pam_console_apply sets or resets permissions on devices in the same manner as pam_console. If /var/run/console.lock exists, pam_console_apply will grant permissions to the user listed therein. If the lock file does not exist, permissions are reset to those listed in /etc/security/console.perms, which should be configured to set permissions on devices so that root owns them. ARGUMENTS
-r Signals pam_console_apply to reset permissions. The default is to set permissions so that the user listed in /var/run/console.lock has access to the devices, and to reset permissions if no such file exists. FILES
/var/run/console.lock /etc/security/console.perms SEE ALSO
pam_console(8) console.perms(5) BUGS
Let's hope not, but if you find any, please report them via the "Bug Track" link at http://bugzilla.redhat.com/bugzilla/ AUTHOR
Nalin Dahyabhai <nalin@redhat.com>, using code shamelessly stolen from parts of pam_console. Red Hat 2001/3/6 pam_console_appy(8)
All times are GMT -4. The time now is 07:28 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy