1. Home
  2. Data & files

Data files · FTP

Listing Files on an FTP Server: LIST, NLST and MLSD

Eigenfield editorial team ·

Before a script can download a data file from an FTP server it has to know what is there. Listing a remote directory is a separate step from transferring, and it is the step where most automation trips: the reply format varies between servers, names can contain spaces, and a listing taken at one moment may be stale a minute later. This page describes the three listing commands, shows how to read each reply, and explains how to pick files by name or time stamp. It follows from opening an FTP connection and is one of the tasks grouped under Data and files.

Three commands, three kinds of reply

NLST returns names only, one per line. It is the easiest to parse and the least informative: no sizes, no dates, and no way to tell a file from a directory. LIST returns whatever the server’s own directory command prints. On many servers that looks like a Unix long listing, with permissions, a link count, an owner, a size, a date and a name; on others it resembles a different operating system’s format. Because no standard governs it, a parser written for one server may fail on the next.

MLSD is the machine-readable option. Each line holds a set of facts as key=value pairs, such as type, size and modify, followed by a space and the name. The modify value is a time stamp in a fixed year-month-day-hour-minute-second form in UTC. Not every server supports MLSD, so a careful script tries it first and falls back to LIST or NLST.

from ftplib import FTP
ftp = FTP("ftp.example.com", timeout=30)
ftp.login()                       # anonymous, if the server allows it
ftp.cwd("/pub/data")
try:
    entries = list(ftp.mlsd(facts=["type", "size", "modify"]))
except Exception:
    entries = [(n, {}) for n in ftp.nlst()]

Parsing a LIST reply

When MLSD is unavailable, a LIST line can be split on whitespace into at most eight pieces, leaving the remainder as the name. That one rule handles file names with spaces. A Unix-style line such as -rw-r--r-- 1 ftp ftp 20480 Mar 03 14:22 run7.dat gives the type from the first character, a size of 20480 bytes, and the name run7.dat. A leading d marks a directory and an l marks a link.

The date is the awkward field. Recent files show month, day and clock time, with no year; older ones show month, day and year. A script that needs correct dates must infer the year, usually by assuming that a time-only date is in the past six months. If exact times matter, ask for them with MLSD or a separate modification-time command instead of guessing.

Selecting the files you need

Filter in the script, not by hand. Match names with a pattern such as run*.dat, drop directories, and sort by modification time to find the newest. Selecting by size can catch truncated uploads: a data file expected to be about 20 KB that shows up with 0 bytes is a file still being written.

A pattern does not need to be complicated. The question to ask is how the files are named when they are complete, and whether any temporary name is used while the upload is under way. If the sender writes to a temporary name and renames at the end, matching only the final name avoids half-written files entirely.

Habits worth keeping

  • Set a timeout, so a stalled server cannot hang the script.
  • Use passive mode when a firewall or router sits between the two machines; most client libraries enable it by default.
  • Close the connection in every outcome, including errors, as described under trapping errors in scripts.
  • Record the listing, not just the files fetched. A saved listing explains later why a file was or was not picked up, and two listings can be compared with the approach in comparing two files.

Plain FTP sends passwords and data unencrypted. Where the server supports an encrypted variant, prefer it, and never send valuable credentials over the plain protocol.

Back to top