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
FILEPERMS(3)								 1							      FILEPERMS(3)

fileperms - Gets file permissions

SYNOPSIS
int fileperms (string $filename) DESCRIPTION
Gets permissions for the given file. PARAMETERS
o $filename - Path to the file. RETURN VALUES
Returns the file's permissions as a numeric mode. Lower bits of this mode are the same as the permissions expected by chmod(3), however on most platforms the return value will also include information on the type of file given as $filename. The examples below demonstrate how to test the return value for specific permissions and file types on POSIX systems, including Linux and Mac OS X. For local files, the specific return value is that of the st_mode member of the structure returned by the C library's stat(3) function. Exactly which bits are set can vary from platform to platform, and looking up your specific platform's documentation is recommended if parsing the non-permission bits of the return value is required. EXAMPLES
Example #1 Display permissions as an octal value <?php echo substr(sprintf('%o', fileperms('/tmp')), -4); echo substr(sprintf('%o', fileperms('/etc/passwd')), -4); ?> The above example will output: 1777 0644 Example #2 Display full permissions <?php $perms = fileperms('/etc/passwd'); if (($perms & 0xC000) == 0xC000) { // Socket $info = 's'; } elseif (($perms & 0xA000) == 0xA000) { // Symbolic Link $info = 'l'; } elseif (($perms & 0x8000) == 0x8000) { // Regular $info = '-'; } elseif (($perms & 0x6000) == 0x6000) { // Block special $info = 'b'; } elseif (($perms & 0x4000) == 0x4000) { // Directory $info = 'd'; } elseif (($perms & 0x2000) == 0x2000) { // Character special $info = 'c'; } elseif (($perms & 0x1000) == 0x1000) { // FIFO pipe $info = 'p'; } else { // Unknown $info = 'u'; } // Owner $info .= (($perms & 0x0100) ? 'r' : '-'); $info .= (($perms & 0x0080) ? 'w' : '-'); $info .= (($perms & 0x0040) ? (($perms & 0x0800) ? 's' : 'x' ) : (($perms & 0x0800) ? 'S' : '-')); // Group $info .= (($perms & 0x0020) ? 'r' : '-'); $info .= (($perms & 0x0010) ? 'w' : '-'); $info .= (($perms & 0x0008) ? (($perms & 0x0400) ? 's' : 'x' ) : (($perms & 0x0400) ? 'S' : '-')); // World $info .= (($perms & 0x0004) ? 'r' : '-'); $info .= (($perms & 0x0002) ? 'w' : '-'); $info .= (($perms & 0x0001) ? (($perms & 0x0200) ? 't' : 'x' ) : (($perms & 0x0200) ? 'T' : '-')); echo $info; ?> The above example will output: -rw-r--r-- ERRORS
/EXCEPTIONS Upon failure, an E_WARNING is emitted. NOTES
Note The results of this function are cached. See clearstatcache(3) for more details. Tip As of PHP 5.0.0, this function can also be used with some URL wrappers. Refer to "Supported Protocols and Wrappers" to determine which wrappers support stat(3) family of functionality. SEE ALSO
chmod(3), is_readable(3), stat(3). PHP Documentation Group FILEPERMS(3)
All times are GMT -4. The time now is 05:23 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy