ftp only older files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ftp only older files
# 1  
Old 05-29-2009
ftp only older files

Hi All,

I want to get those the files which were created before 20 days.
Say, we have two server, remote server and local server.

I want to get only 20 days older files from remote server to my local server.

I have used following code for ftp:

(echo "
user ${USERNAME} ${PASSWD}
lcd ${LOCAL_DATA}
cd ${REMOTE_FTP_PATH}
mget * ---------->> Here I get all files but I need 20 days older files.
bye
") | ftp -in ${REMOTE_FTPHOST}

Is there any code or command for the ftp so that I can get only the older files?

Thanks in advance.
# 2  
Old 05-29-2009
ftp does not have an option to do what you are looking for it to do.

It can be done with a bit of scripting however.
- ftp to remote server
- get a list of files with date/times (dir)
- close connection
- process list to select files older than 20 days
- ftp to remote server
- get files on processed list
- close connection
# 3  
Old 05-29-2009
if you have Python,
Code:
#!/usr/bin/env python
import ftplib,datetime,time
today = datetime.date.today()

server="localhost"
user="anonymous"
password="test@hotmail.com"
filelist=[]
months={
 "Jan":"01","Feb":"02","Mar":"03","Apr":"04","May":"05", 
 "Jun":"06","Jul":"07","Aug":"08","Sep":"09","Oct":"10",
 "Nov":"11","Dec":"12"
}

def download(ftp, filename):    
    '''function to download file'''
    ftp.retrbinary("RETR " + filename, open(filename,"wb").write)
    
try:
    ftp = ftplib.FTP()
    ftp.connect(server,21)    
    ftp.login(user,password)
except Exception,e:
    print e
else:    
    ftp.cwd("incoming")    
    ftp.retrlines('LIST',filelist.append)    
    for items in filelist:
        items=items.split()
        mth,day,yr = items[5:8]        
        if ":" in yr:
            yr=today.year        
        thedate = datetime.date(int(yr),int(months[mth]),int(day))
        result = today - thedate 
        if  result.days > 20:
            filename =''.join(items[8:])
            print "downloading .. " , filename
            download(ftp,filename)

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to create zip/gz/tar files for if the files are older than particular days in UNIX or Linux?

I need a script file for backup (zip or tar or gz) of old log files in our unix server (causing the space problem). Could you please help me to create the zip or gz files for each log files in current directory and sub-directories also? I found one command which is to create gz file for the... (4 Replies)
Discussion started by: Mallikgm
4 Replies

2. UNIX for Advanced & Expert Users

Help with get/mget from FTP server with files older than 10 minutes

Hi! I am new to unix and this forum as well.. Can someone please help me : I want to "get/mget" files which are older than 10 minutes from a remote FTP server like "ftp.com". After getting the files to local unix server say "Prod.com" , i need to delete only those files from ftp.com which... (4 Replies)
Discussion started by: SravsJaya
4 Replies

3. Shell Programming and Scripting

How to move the older than 30 file to another ftp server

Hi All I need to move the older than 30 days file to another ftp server. I have source structure like this Files folder Folder1 Folder2 Folder3 I need to create same Target structure and I need to move the older than 30 day file to another ftp server, can you please suggest me how I develop... (1 Reply)
Discussion started by: murari83.ds
1 Replies

4. Shell Programming and Scripting

Delete files older than 1 year through FTP

Hi All, I want to login to a remote server using FTP command and then check for files older than 1 year and delete those files. Please let me know how can i achieve this using Unix Commands. Thanks in Advance, (10 Replies)
Discussion started by: HemaV
10 Replies

5. Shell Programming and Scripting

how to delete the older files other than the recently added 5 files

Number of files will get created in a folder automatically daily.. so i hav to delete the older files other than the recently added 5 files.. Could u help me through this..?? (5 Replies)
Discussion started by: shaal89
5 Replies

6. UNIX for Dummies Questions & Answers

Files older than 50 days

Hi All, OS :- HP-UX wm5qa B.11.23 U ia64 1119805695 unlimited-user license I need to search files older than 50 days. I've used following command in order to search desired files, I also discoverd, it's showing today's files as well. Do you have any clue with this ? wmqa1> find .... (4 Replies)
Discussion started by: alok.behria
4 Replies

7. Shell Programming and Scripting

Automatic FTP-Download | not files older then x days

Hey Guys, i need to download files from a ftp-server that not older than $VAR (x) days eg for seven days ./script 7 or two weeks ./script 14 all files from the last 14 days will download but how i can explain "ftp" this? sorry for my strange english :/ (2 Replies)
Discussion started by: tetex
2 Replies

8. Shell Programming and Scripting

rm files older then 2 seconds?

Hello, I've got a script to delete 0 byte files, but I need it to work only for files that have been created at least 2 seconds ago (Are two seconds old). I'm not sure what's the best way of doing this, I've had a look at the stat command too but well.. for file in `ls -l | grep ^- |... (7 Replies)
Discussion started by: TehOne
7 Replies

9. UNIX for Dummies Questions & Answers

Reg: delete older files from ftp

Hi, I want to delete older files from ftp server. (files which are more than 5 days old). Please advice Thanks , sam (3 Replies)
Discussion started by: sam99
3 Replies

10. UNIX for Dummies Questions & Answers

rm files older than ...

Hello, How can I remove files older than yesterday or the day before or a given day ... Thank you in advance (2 Replies)
Discussion started by: annemar
2 Replies
Login or Register to Ask a Question