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
- Step 1
1.lftp opens a control connection to TCP 21
- Step 2
2.Sends USER and PASS
- Step 3
3.Server replies 230 Login successful
- Step 4
4.PASV gives a data port
- Step 5
5.Listing or file travels on a separate passive data connection
- Step 1
1.lftp starts ssh as a connect program (ssh -a -x -s -l student lab-srv sftp)
- Step 2
2.ssh builds an encrypted tunnel on TCP 22
- Step 3
3.SFTP packets (INIT, OPENDIR, READDIR) travel inside it
- Step 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
| Option | Meaning |
|---|---|
| SITE / URL | Host, URL (ftp, sftp, ftps, http, fish) or bookmark |
| -u USER,PASS | User and password (-u USER alone: prompt or .netrc) |
| -p PORT | Port |
| -e 'CMDS' | Run commands, then stay at the prompt |
| -c 'CMDS' | Run commands and exit (cron/scripts); cannot be mixed with -u |
| -f FILE | Run a script file and exit |
| -d | Debug: every command and reply |
| --norc / --rcfile F | Skip rc files / run this rc file |
| --env-password | Password from LFTP_PASSWORD |
| -s SLOT / -B | Slot / no bookmark lookup |
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'$ lftp -u student,lab150pw -c 'cd pub'lftp: -c, -f, -v, -h conflict with other `open' options and arguments(exit 1)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$ 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:
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---- 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 3150.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.
cd pub && echo cd workedcd nope || echo cd failedcd logs; cls -1!hostnamealias ll "cls -l"llaliasglob echo *.logsource /lab/setup.lftphistorylftp 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 history150.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.
set net:limit-rate 512Kget big.bin &get big.bin -o copy.bin &jobskill 1jobsfg 0!ls -l /dllftp 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.bin150.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.
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 listlftp 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_log150.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.
pwdcd publcd /dllsfinddu -hglob -d echo *cd logscd -pwdlftp student@lab-srv:~/pub> cd logslftp student@lab-srv:~/pub/logs> cd -lftp student@lab-srv:~/pub> pwdftp://student:lab150pw@lab-srv/%2Fhome/student/pub150.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.
cls -l --sort=sizecls -lhcls -1s --block-size=1024cls -D -1cls -l --sort=datelftp 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 README150.9 Downloading: get, reget, mget, get1, cat
| Command | Use |
|---|---|
| get [-c] file [-o name] | Download; -c continues a partial file |
| reget file | Resume (same as get -c) |
| mget pattern | Many files with wildcards (-P N parallel, -O dir) |
| get1 -o name file | Low-level single-file transfer (--source-region) |
| pget -n N file | One file in N parallel chunks (-c resumes) |
| cat / zcat / more | Print / unzip and print / page a remote file, no local copy |
| edit file | Download, open in editor, upload back if changed |
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 /dllftp 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- Step 1
1.pget splits big.bin (4 MB) into 4 byte ranges
- Step 2
2.Chunk 1: 0-1 MB on its own FTP connection
- Step 3
3.Chunk 2: 1-2 MB on a second connection
- Step 4
4.Chunks 3 and 4 download at the same time
- Step 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:
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.binlftp 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.bincat README report.txtzcat logs/access.log.gzmore sales.csvlftp student@lab-srv:~/pub> more sales.csvregion,ordersnorth,700south,55034 bytes transferred150.10 Comparison: lftp vs plain ftp client
| Point | lftp | ftp (tnftp / ftp.exe) |
|---|---|---|
| Protocols | ftp, ftps, sftp, http, fish, torrent | FTP (tnftp also http) |
| Broken transfer | Retries and resumes automatically | Manual reget |
| Parallelism | Background jobs, pget, mget -P | One 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.