Ftp – How to find timestamp of file on FTP server

ftppythontimestamp

I need to find the latest created file in a FTP folder. However the FTP server is not returning full timestamps for files with the LIST command (missing the year):

drwxr-xr-x   2 owner    group               0 Nov  9 17:29 archive
drwxr-xr-x   2 owner    group               0 Nov  9 17:35 category
drwxr-xr-x   2 owner    group               0 Jan  9 07:21 images

And the MLSD command is not supported.

So currently I check the timestamp of each file with the MDTM command. Is there a more efficient way?

I am using the ftplib wrapper.

Best Answer

man ftp says:

 ls [remote-directory] [local-file]
             Print a listing of the contents of a directory on the remote machine.
             The listing includes any system-dependent information that the server
             chooses to include; for example, most UNIX systems will produce output
             from the command ‘ls -l’.  (See also nlist.)  If remote-directory is
             left unspecified, the current working directory is used.  If interac‐
             tive prompting is on, ftp will prompt the user to verify that the last
             argument is indeed the target local file for receiving ls output.  If
             no local file is specified, or if local-file is ‘-’, the output is
             sent to the terminal.

So, most UNIX systems will produce output from the command ‘ls -l’ and such command either shows date or date and time, depending on the “age” of file:

-rw-rw-r-- 1 user group        4 ene 21 12:40 keepalive.out
-rw-rw-r-- 1 user group   525292 oct  4  2013 Linux.svg

Taking into account you are after the FTP command to get the timestamps reliably and efficiently, I'm affraid your approach (MDTM command) is the only one not including programming stuff.