SkillPilotAI

lftp (1/3)

lftp is a powerful, scriptable command-line client that runs FTP, FTPS, SFTP, HTTP, FISH and BitTorrent from one shell. It retries and resumes broken transfers on its own, runs background jobs in parallel and does segmented downloads with pget.

150.1 What it does, where it runs

  • One shell, many protocols, automatic retry/resume, background jobs, mirror.
  • Linux: apt install lftp / dnf install lftp. macOS: brew install lftp (not built in). Windows: not native, use WSL or Cygwin.
  • Lab: n150-cli 192.168.170.20 (lftp 4.9.2), n150-srv 192.168.170.10 (vsftpd :21, sshd :22).

150.2 How it works: FTP vs SFTP URLs

ftp:// URL
  1. 1.lftp opens a control connection to TCP 21

  2. 2.Sends USER and PASS

  3. 3.Server replies 230 Login successful

  4. 4.PASV gives a data port

  5. 5.Listing or file travels on a separate passive data connection

sftp:// URL
  1. 1.lftp starts ssh as a connect program (ssh -a -x -s -l student lab-srv sftp)

  2. 2.ssh builds an encrypted tunnel on TCP 22

  3. 3.SFTP packets (INIT, OPENDIR, READDIR) travel inside it

  4. 4.Both ftp:// and sftp:// work from the same lftp shell

Security comes from the protocol, not from lftp

150.3 Command-line options

OptionMeaning
SITE / URLHost, URL (ftp, sftp, ftps, http, fish) or bookmark
-u USER,PASSUser and password (-u USER alone: prompt or .netrc)
-p PORTPort
-e 'CMDS'Run commands, then stay at the prompt
-c 'CMDS'Run commands and exit (cron/scripts); cannot be mixed with -u
-f FILERun a script file and exit
-dDebug: every command and reply
--norc / --rcfile FSkip rc files / run this rc file
--env-passwordPassword from LFTP_PASSWORD
-s SLOT / -BSlot / no bookmark lookup
Terminal
lftp --versionlftp -u student,lab150pw -e 'cd pub; cls' lab-srvlftp -c 'open -u student,lab150pw lab-srv; cd pub; cls -1 logs'lftp -u student,lab150pw -c 'cd pub'
Output
$ lftp -u student,lab150pw -c 'cd pub'lftp: -c, -f, -v, -h conflict with other `open' options and arguments(exit 1)
Terminal
lftp -u student,lab150pw -p 22 -e 'pwd; cls pub; bye' sftp://lab-srvcat /lab/daily.lftp; lftp -f /lab/daily.lftpecho 'echo rc file loaded' > ~/.lftprclftp -c 'echo hello'lftp --norc -c 'echo hello'lftp -u student,wrongpw -e 'ls; bye' lab-srv
Output
$ lftp -u student,wrongpw -e 'ls; bye' lab-srvls: Login failed: 530 Login incorrect.(exit 1)

With -d an FTP session shows the banner, FEAT, USER, PASS, 230, PASV and LIST; an SFTP session shows only the ssh connect program and binary packets:

Terminal
lftp -d -u student,lab150pw -e 'ls pub/README; bye' lab-srvlftp -d -u student,lab150pw -e 'cls -1 pub/logs; bye' sftp://lab-srv
Output
---- Running connect program (ssh -a -x -s -l student lab-srv sftp)---> sending a packet, length=5, type=1(INIT), id=0<--- student@lab-srv's password: XXXX<--- got a packet, length=318, type=2(VERSION), id=0---- protocol version set to 3

150.4 Shell features

  • && runs the next command only on success, || only on failure; ; separates commands.
  • ! runs a local shell command (hostname printed cli).
  • alias ll "cls -l" creates a shortcut; alias lists built-in aliases too.
  • glob expands a pattern for a command; source runs a file of commands; history keeps every command with its time.
  • Redirects work: ls > file, ls | cmd; ( CMDS ) & groups commands into one background job.
Output
cd pub && echo cd workedcd nope || echo cd failedcd logs; cls -1!hostnamealias ll "cls -l"llaliasglob echo *.logsource /lab/setup.lftphistory
Output
lftp student@lab-srv:~/pub/logs> !hostnameclilftp student@lab-srv:~/pub> history    4  2026-11-09 06:18:53  open -u student,lab150pw lab-srv    5  2026-11-09 06:18:54  cd pub && echo cd worked    6  2026-11-09 06:18:55  cd nope || echo cd failed   12  2026-11-09 06:19:03  glob echo *.log   13  2026-11-09 06:19:04  source /lab/setup.lftp   14  2026-11-09 06:19:05  history

150.5 Jobs and scripting

  • cmd & starts a background job; jobs (-v) shows progress; kill N stops a job; fg N / wait N waits for it.
  • In the lab job 0 finished big.bin (4194304 bytes) while killed job 1 left copy.bin half done.
  • at 23:00 -- CMD runs at a time; attach PID reconnects to a backgrounded lftp; exit with running jobs moves lftp to the background (exit kill stops them).
  • repeat [--until-ok] runs a command again and again; glob --exist tests patterns; eval builds commands.
Output
set net:limit-rate 512Kget big.bin &get big.bin -o copy.bin &jobskill 1jobsfg 0!ls -l /dl
Output
lftp student@lab-srv:~/pub> !ls -l /dl-rw-r--r--  1 root  root  4194304 Nov  9 06:17 big.bin-rw-r--r--  1 root  root  2244840 Nov  9 06:19 copy.bin

150.6 Connection, sessions and bookmarks

  • open selects a server; user sets login details.
  • bookmark add NAME saves the current location; bookmark list hides the password as XXXX; bookmark del removes one.
  • scache lists cached sessions (FTP and SFTP kept ready); scache N switches.
  • slot NAME creates a virtual console; close -a closes all idle connections.
  • Bookmarks are stored in ~/.local/share/lftp/bookmarks; cache flush clears cached listings; quote sends a raw command; debug 3 / -o FILE controls logging.
Output
open lab-srvuser student lab150pwcd pubbookmark add labftpopen -u student,lab150pw sftp://lab-srv/home/student/pub/logsbookmark add labsftpbookmark listscacheslot workopen labftpclose -abookmark del labsftpbookmark list
Output
lftp student@lab-srv:~/pub/logs> scache0  ftp://student:lab150pw@lab-srv/%2Fhome/student1  ftp://student:lab150pw@lab-srv/%2Fhome/student/pub2  sftp://student:lab150pw@lab-srv/home/student/publftp work student@lab-srv:~/pub> bookmark listlabftp  ftp://student:XXXX@lab-srv/%2Fhome/student/publftp work student@lab-srv:~/pub> bye$ ls ~/.local/share/lftp/bookmarks  cwd_history  rl_history  transfer_log

150.7 Navigation and listing

  • pwd shows the full remote URL; cd changes the remote folder, cd - goes back; lcd / lpwd for the local side.
  • ls is the server's own listing; find lists the whole tree; du -h shows human-readable sizes.
  • rels / renlist ignore the cache; nlist gives names only; glob -d matches folders only.
Output
pwdcd publcd /dllsfinddu -hglob -d echo *cd logscd -pwd
Output
lftp student@lab-srv:~/pub> cd logslftp student@lab-srv:~/pub/logs> cd -lftp student@lab-srv:~/pub> pwdftp://student:lab150pw@lab-srv/%2Fhome/student/pub

150.8 cls: client-formatted listing

  • The server decides the format of ls, but lftp formats cls itself, so it looks the same on every server.
  • --sort=size / date, -h human sizes, --block-size=1024, -D folders first, -F type indicators, --time-style for date format.
  • -a dot files, -B base names, -d the dir entry itself, --si powers of 1000.
Output
cls -l --sort=sizecls -lhcls -1s --block-size=1024cls -D -1cls -l --sort=date
Output
lftp student@lab-srv:~/pub> cls -l --sort=date-rw-r--r--  1 1000  1000  4194304 Nov  9 06:17 big.bin-rw-r--r--  1 1000  1000       34 Nov  8 18:00 sales.csv-rw-r--r--  1 1000  1000       29 Nov  5 09:30 report.txt-rw-r--r--  1 1000  1000       31 Nov  1 10:00 README

150.9 Downloading: get, reget, mget, get1, cat

CommandUse
get [-c] file [-o name]Download; -c continues a partial file
reget fileResume (same as get -c)
mget patternMany files with wildcards (-P N parallel, -O dir)
get1 -o name fileLow-level single-file transfer (--source-region)
pget -n N fileOne file in N parallel chunks (-c resumes)
cat / zcat / morePrint / unzip and print / page a remote file, no local copy
edit fileDownload, open in editor, upload back if changed
Output
set net:limit-rate 200Kget big.bin &get big.bin -o r.bin &kill allset net:limit-rate 0get -c big.binreget big.bin -o r.binmget logs/*.logget1 -o q3.txt report.txtget missing.txt!ls /dl
Output
lftp student@lab-srv:~/pub> get -c big.bin3501193 bytes transferredlftp student@lab-srv:~/pub> reget big.bin -o r.bin3768810 bytes transferredlftp student@lab-srv:~/pub> get missing.txtget: Access failed: 550 Failed to open file. (missing.txt)lftp student@lab-srv:~/pub> !ls /dlbig.bin  mon.log  q3.txt  r.bin  tue.log  wed.log
pget -n 4: one file, 4 connections
  1. 1.pget splits big.bin (4 MB) into 4 byte ranges

  2. 2.Chunk 1: 0-1 MB on its own FTP connection

  3. 3.Chunk 2: 1-2 MB on a second connection

  4. 4.Chunks 3 and 4 download at the same time

  5. 5.Pieces are joined into one file: same sha256

With 100 KB/s per connection, the 4 chunks gave ~400 KB/s total and the file arrived in 9 seconds:

Output
set net:limit-rate 100Kpget -n 4 big.bin -o p.bin &jobs -vwait 0!ls -l /dl!sha256sum /dl/big.bin /dl/r.bin /dl/p.bin
Output
lftp student@lab-srv:~/pub> wait 04194304 bytes transferred in 9 seconds (436.5 KiB/s)$ sha256sum /dl/big.bin /dl/r.bin /dl/p.binde339611...71aa5  /dl/big.binde339611...71aa5  /dl/r.binde339611...71aa5  /dl/p.bin
Output
cat README report.txtzcat logs/access.log.gzmore sales.csv
Output
lftp student@lab-srv:~/pub> more sales.csvregion,ordersnorth,700south,55034 bytes transferred

150.10 Comparison: lftp vs plain ftp client

Pointlftpftp (tnftp / ftp.exe)
Protocolsftp, ftps, sftp, http, fish, torrentFTP (tnftp also http)
Broken transferRetries and resumes automaticallyManual reget
ParallelismBackground jobs, pget, mget -POne transfer at a time
Scripting-c, -f, source, ~/.lftprc, &&, ||Scripts / .netrc macros

150.11 Common mistakes

  • Using -e in cron: lftp stays at the prompt and the job never ends; use -c.
  • Mixing -c with -u: real conflict error; put open -u inside the -c string.
  • Thinking lftp makes FTP secure: PASS goes in plain text on ftp://; use sftp:// or ftps://.
  • Forgetting to reset net:limit-rate after testing.

150.12 Interview tips

  • lftp beats ftp: many protocols in one shell, automatic retry/resume, background jobs, pget segmented downloads, full scripting with -c.
  • Cron job: lftp -c 'open -u user,pass host; get file' so it exits after the work.
  • pget -n N opens N connections on byte ranges of one file to speed up a single big download.
  • cls is formatted by the client, so listings look the same across servers.