Ftp get files created in last 30 minutes

Is it possible in an ftp script to get remote files based on whether they have been created in the last 30 minutes?

Yes, it's perfectly possible.
Just a question based on a previous experience with ftp. Are you sure you want to do this using ftp? No chance of using sftp instead?
What OS are you on?
I'll be more than glad to help you.

At the moment sftp is not stup on the remote host so ftp would be best at this stage.

operataing system is Aix6.1

Thanks in advance!

You may have to tweak this a little bit as I am on a Linux box and don't have an AIX one to test. But at least I believe this should get you going. Also, I'm no Perl expert but hopefully you will get the point ;). The process is performed in 2 steps:
1) Connect to the remote host using ftp and download a listing of the remote directory. Save each file's last modification time and file name to files Dates.txt and Files.txt, respectively.
2) Compare the modification time of each file against the current time minus 30 minutes and if the condition is satisfied, connect again and download the file.

#!/bin/bash

# Login credentials
user='ftp-user-name' #Do not forget to enclose inside single or double quotes
pass='ftp-password'
directory='/absolute/path/to/remote/ftp/directory/'
host='IP Address or hostname of remote host'

# Set time
# perl -e 'print time' will return the current time in Unix epoch format
# So if you substract 43200, that should give you the current time minus 30 minutes in Unix epoch format:
time=$(perl -e 'print time-43200')

# Connect to host and download the listing of the remote directory
ftp -n $host <<END_GET_LIST
	quote USER $user
	quote PASS $pass
	cd $directory
	ls -l ftpList.txt
	quit
END_GET_LIST
# Disconnect from remote host

# Save the 6th, 7th, and 8th field of the directory listing (i.e. Aug 15 5:15) of each line into file Dates.txt
awk -F ' ' '{print $6,$7,$8}' ftpList.txt > 'Dates.txt'

# Save the 9th field of the directory listing (file name) of each line into file Files.txt
awk -F ' ' '{print $9}' ftpList.txt > 'Files.txt'

linenum=0 #Auxiliary variable for sed.

while read list; do
	linenum=$((linenum+1))
	#Convert the modification datetime of each file to Unix's epoch
	epoch=$(perl -MFile::stat -e "print stat('$list')->mtime")
	if [ $epoch -gt $time ] ; then
		file=$(sed -n "${linenum}p" Files.txt)	#If the condition is satisfied, use sed to get the name of the file
												#in the same line of Files.txt.
#Connect again and download file when the condition above has been satisfied.
		ftp -n $host <<END_RETRIEVE
			quote USER $user
			quote PASS $pass
			cd $directory
			get $file
			quit
END_RETRIEVE
	fi
done < 'Dates.txt'

If someone else in these forums can come with a better way to do this, we'd all be thankful to see it :).

Thanks gacanepa, this is ideal. for some reason it didn't like the print stat ('$list') so I I copied $list into a file and referenced the file instead of variable and it worked strangely:confused:

Can you please copy here the output?

And please run this command in your CLI, replacing $list with whatever local file you want:

perl -MFile::stat -e "print stat('$list')->mtime"

It should look like this:

perl -MFile::stat -e "print stat('yourfile')->mtime"

And also copy the result of that command.