0

I have access to an FTP server where clients are uploading an updated file twice a week. I have to write a bash script to check if the file (I know the name of file) was modified in last 24hrs: if yes, then I need to download that file.

I am able to normally download a file from FTP using a bash script, but I am not sure how to check the last modified time in FTP.

user35915
  • 1,222
  • 3
  • 17
  • 23

2 Answers2

1

In most cases FTP ls command is linked to server ls command. So you should try

ls -l <filename>
samarkand
  • 47
  • 1
  • 9
1

The easiest way to do it is probably with curl, using the -z/--time-cond option:

(HTTP/FTP) Request a file that has been modified later than the given time and date, or one that has been modified before that time. The can be all sorts of date strings or if it doesn't match any internal ones, it is taken as a filename and tries to get the modification date (mtime) from instead. See the curl_getdate(3) man pages for date expression details.

The accepted date formats are specified in curl_getdate docs, but it suffices to say the usual English-natural RFC-822 format is supported, which is also supported in date util via -R/--rfc-2822 option.

We can calculate the date/time of the cut-off timestamp 24 hour ago with a relative date string:

$ date -R -d '24 hours ago'
Mon, 25 Sep 2017 17:30:49 +0200

and supply that value to curl:

curl 'ftp://server/file' --time-cond "$(date -R -d '24 hour ago')"

To save the output directly to a local file (and not output it to stdout), use the --output filename option, and to suppress the progress output, use the -s option.

randomir
  • 17,989
  • 1
  • 40
  • 55