lftp (2/3)
Part 2 of lftp covers uploading and managing remote files, the job queue, mirror (folder sync in both directions), the built-in torrent client and the set command with its settings (bmk, cache, color, cmd, dns, file, module, fish, sftp). This is what you use to deploy a website or keep a server folder in sync from a script.
151.1 Why is it used?
- Upload single files or whole wildcards over FTP or SFTP, with resume when a big upload breaks.
- Rename, chmod and delete files on the server without logging in to a shell there.
- Queue many transfers to one site so they run one after another (or N in parallel).
- mirror syncs a whole folder tree: download (default) or upload with -R, with --delete, --only-newer and filters.
- Settings (set) make scripts safe: cmd:fail-exit stops on error, sftp:auto-confirm handles new host keys.
151.2 How it works: lab and upload path
- Lab: lftp 4.9.2 client on 192.168.171.10, server 192.168.171.20 with vsftpd (FTP, port 21, passive ports 30000-30010) and sshd/sftp (port 22).
- FTP: lftp logs in on the control connection (port 21), then each file goes on a separate data connection.
- SFTP (sftp:// URL): everything (login, commands, data) runs in one encrypted SSH connection on port 22.
- Step 1
1.Client 192.168.171.10 runs: put index.html
- Step 2
2.FTP: USER / PASS on the control connection to 192.168.171.20:21
- Step 3
3.Server replies to PASV with a port in 30000-30010
- Step 4
4.STOR index.html goes on that data connection
- Step 5
5.Server: OK UPLOAD: /www/index.html
- Step 6
6.With sftp://alice@srv the same upload runs inside one encrypted TCP 22 connection
FTP = 2 connections (control + data, plain text); SFTP = 1 encrypted connection
151.3 Uploading: put, mput, mkdir
- put FILE uploads one file; put -o NAME gives it a new remote name.
- mput expands wildcards (mput *.html); mkdir -p creates the whole path.
- put -O BASE uploads into a remote base directory; mput -P 3 uploads 3 files in parallel (in 4.9.2 -P works with mput).
put index.htmlput notes.txt -o readme.txtmput *.htmlmkdir -p assets/css/v1put -O assets/css style.cssmput -P 3 a.bin b.bin c.binmput *.tmpcls -llftp alice@srv:/www> cls -l-rw-r--r-- 1 1000 1000 2 Nov 9 06:43 a.bin-rw-r--r-- 1 1000 1000 15 Nov 9 06:43 about.htmldrwxr-sr-x 3 1000 1000 4096 Nov 9 06:43 assets/-rw-r--r-- 1 1000 1000 2 Nov 9 06:43 b.bin-rw-r--r-- 1 1000 1000 2 Nov 9 06:43 c.bin-rw-r--r-- 1 1000 1000 17 Nov 9 06:43 index.html-rw-r--r-- 1 1000 1000 6 Nov 9 06:43 readme.txt-rw-r--r-- 1 1000 1000 2 Nov 9 06:43 x.tmp151.4 Upload resume: put -c and reput
- Two 2 MB uploads were limited to 300K and killed after 2 seconds, so only about 850,000 bytes reached the server.
- put -c and reput both continue from where the server copy stops and finish all 2,000,000 bytes.
timeout 2 lf 'set net:limit-rate 300K' \ 'put big.bin'timeout 2 lf 'set net:limit-rate 300K' \ 'put big2.bin'lf 'cls -l big.bin big2.bin'lf 'put -c big.bin' 'reput big2.bin' \ 'cls -l big.bin big2.bin'lftp alice@srv:/www> put -c big.binlftp alice@srv:/www> reput big2.binlftp alice@srv:/www> cls -l big.bin big2.bin-rw-r--r-- 1 1000 1000 2000000 Nov 9 06:43 big.bin-rw-r--r-- 1 1000 1000 2000000 Nov 9 06:43 big2.bin151.5 Remote file management: mv, chmod, rm, mrm, rmdir, ln, edit
- mv renames (readme.txt -> docs.txt); chmod -v reports every change.
- chmod 644 *.html failed on FTP: the wildcard went to the server as it is, so list names separately.
- mrm deletes with a wildcard; rmdir removes only an empty folder (non-empty gives 550), rm -r removes a tree.
- ln on FTP: unknown SITE command (vsftpd has no links). Over sftp ln -s made a symlink, a hard link gave 'operation not supported'.
- edit downloads the file, runs $EDITOR, then uploads the changed file back.
mv readme.txt docs.txtchmod -v 600 docs.txtchmod 644 *.htmlrm c.binmrm *.tmpmkdir -p old/logsrmdir old/logsrmdir oldmkdir -p tmp/xrmdir tmprm -r tmpEDITOR='sed -i s/v1/v2/' \ lf 'edit index.html' 'cat index.html'lftp alice@srv:/www> rmdir tmprmdir: Access failed: 550 Remove directory operation failed. (tmp)lftp alice@srv:/www> rm -r tmplftp alice@srv:/www> edit index.htmllftp alice@srv:/www> cat index.html<h1>Home v2</h1>| Option | Meaning |
|---|---|
| put -o RFILE | Remote file name |
| put -c / reput | Continue (resume) upload |
| put -E | Delete the local file after upload |
| put -e | Delete the target first |
| put -a | ASCII mode |
| put -O BASE | Remote base dir or URL |
| mput -d | Create remote dirs too |
| mput -P N | N parallel uploads |
| mkdir -p / -f | Whole path / quiet |
| rm -r / -f | Recursive / hide errors |
| mmv FILES DIR | Move files into a dir (mv f1 f2 dir/ does the same) |
| chmod -R / -v / -c / -f | Recursive / report all / report only changes / quiet |
151.6 Queue
- queue runs commands for one site one after another.
- queue stop: new items do not start; queue -d 3 deletes item 3; queue -m 2 1 moves item 2 before item 1.
- queue start resumes; jobs -v shows progress (q1 at 51%, q2 and q3 waiting); wait all waits for everything.
queue stopqueue put big.bin -o q1.binqueue put big.bin -o q2.binqueue put big.bin -o q3.binqueue -d 3queue -m 2 1queuelftp alice@srv:/www> queueftp://alice@srv/wwwQueue is stopped.Commands queued: 1. put big.bin -o q2.bin 2. put big.bin -o q1.bin| Command | Meaning |
|---|---|
| queue CMD | Add a command |
| queue -n N CMD | Insert before item N |
| queue wait JOBNO | Queue a running job |
| queue stop / start | Pause / resume the queue |
| queue -d [N|PAT] | Delete item (default last) or matching items |
| queue -m ITEM [N] | Move before N (or to the end) |
| queue -q / -v / -Q | Quiet / verbose / re-queueable format |
151.7 mirror
- mirror SRC DST compares two trees and copies the difference: default remote -> local, -R = local -> remote.
- First mirror -R sent 7 files (New: 7 files); the second run sent nothing and printed only the Total line.
- set cmd:interactive yes makes lftp print mirror's Total/New/Removed summary inside a script.
- --dry-run shows the plan; --only-newer --delete gave New 1, Modified 1, Removed 1.
- --exclude-glob / --include-glob filter files; -P 3 transfers 3 files at once; --continue finishes a partial file.
- Step 1
1.Edit files in local site/ (change about, delete notes, add contact)
- Step 2
2.mirror -R --dry-run --delete site site: only print the plan
- Step 3
3.Check the plan: what will be uploaded and removed
- Step 4
4.mirror -R --only-newer --delete site site: real run
- Step 5
5.Summary: New 1, Modified 1, Removed 1
- Step 6
6.Run again: only 'Total: 4 directories, 7 files' (nothing to send)
Without -R the same command downloads server -> local
set cmd:interactive yesmirror -R --verbose site sitemirror -R --dry-run --delete site sitemirror -R --only-newer --delete --verbose site sitemirror --verbose --exclude-glob drafts/ \ --exclude-glob *.tmp site pubmirror --include-glob *.html site html-onlyset net:limit-rate 300Kmirror -P 3 site copy &jobs -vwait alllftp alice@srv:/www> jobs -v[0] mirror -P 3 site copy -- 640k/1.6M (40%) 300.6 KiB/s \mirror `media' -- 580k/1.5M (38%) 300.6 KiB/s \transfer `media/intro.mp4'lftp alice@srv:/www> wait allTotal: 4 directories, 7 files, 0 symlinksNew: 7 files, 0 symlinks1560061 bytes transferred in 8 seconds (189.3 KiB/s)lftp alice@srv:/www> mirror --continue --verbose site dlTransferring file `media/intro.mp4'Total: 4 directories, 7 files, 0 symlinksModified: 1 file, 0 symlinks381999 bytes transferred-rw-r--r-- 1 root root 1500000 Nov 9 06:43 intro.mp4| Group | Key mirror options (60 in total) |
|---|---|
| Direction | -R upload, default download, mirror URL URL = two servers (FXP) |
| Delete | -e/--delete, --delete-excluded, --delete-first |
| Which files | -n/--only-newer, --only-missing, --only-existing, --transfer-all, --ignore-time/--ignore-size, -c/--continue |
| Recursion/links/perms | -r no recursion, --no-empty-dirs, -L dereference, -p no perms, -a (chown + suid + no-umask) |
| Filters | -I/-X glob, -i/-x regex, --newer-than, --older-than, --size-range, --*-from=FILE |
| Parallel/logging | -P N, --use-pget, --on-change, --loop, -v, --log, --script (plan only), --dry-run |
| Source removal | --Remove-source-files, --Remove-source-dirs, --Move |
151.8 torrent
- lftp has a built-in BitTorrent client: torrent FILE|URL|MAGNET downloads and seeds.
- Options: -O DIR, --force-valid, --only-new, --only-incomplete, --dht-bootstrap=NODE, --share PATH (prints a magnet link).
- The lab ran it only without arguments (no internet traffic):
$ lftp -c 'torrent'torrent: Please specify meta-info file or URL.Try `help torrent' for more information.151.9 set command and value types
- set = only changed settings, set -a = all (269 lines), set -d = only defaults.
- set var val sets (the name can be shortened if unique), set var alone unsets.
- Closure: set net:limit-rate/srv 100K applies to one host only.
- Types: time (5h30m, inf, never), bool (on/yes/1 vs off/no/0), tri-bool (bool + auto), int (k/m/g, 0 octal, 0x hex).
- ftpcopy is obsolete: use get/put with two URLs.
lftp -c 'set -a' | wc -llftp -c 'set -d' | grep clobberset cache:expire 5h30mset net:socket-buffer 0x10000set net:limit-rate/srv 100Kset timeout 5set ftp:passive-mode maybeftpcopytimeout: ambiguous variable name. Use `set -a' to look at all variables.maybe: invalid boolean value.Unknown command `ftpcopy'.151.10 Settings: bmk, cache, color, cmd
- bmk:save-passwords yes saved web2's password in the bookmark file: keep it off.
- Cache on: two listings of the same folder send one LIST; cache:enable no sends two. cache:expire default 60m.
- color:use-color yes + color:dir-colors (LS_COLORS format) adds colour codes to cls.
- cmd:trace prints every command with +; cmd:fail-exit yes stops a script with exit code 1 when cd fails.
- cmd:at-queue-finish and cmd:at-exit run commands when the queue ends and when lftp exits.
- cmd:default-protocol sftp makes 'open alice@srv' use SFTP; cmd:verify-path no delays a bad cd error until ls.
set cmd:fail-exit yesset cmd:at-queue-finish \ "!echo queue finished"set cmd:at-exit "!echo bye from at-exit"queue cls -1 q1.bin; wait allecho last commandq1.binqueue finishedlast commandbye from at-exit151.11 Settings: dns, file, module, fish, sftp
- dns:name/web 192.168.171.20 makes the name web point to that IP (an alias inside lftp).
- dns:order default is "inet6 inet"; with inet6 only, the name gave 'No address found'.
- sftp on a new host fails with host key verification failed in a script; sftp:auto-confirm yes accepts and saves the key.
- sftp:protocol-version (default 6), sftp:max-packets-in-flight (16, raise on high RTT), sftp:connect-program (add -i key).
- fish:// lists files over a plain ssh shell; fish:auto-confirm works like the sftp one.
set dns:name/web 192.168.171.20open ftp://alice@web/www; cls -1 *.htmlset dns:order inet6set sftp:auto-confirm yesopen sftp://alice@srv; cls -1set fish:auto-confirm yesopen fish://alice@srv; cls -1 www/*.htmlopen: web: No address foundNot connected www/about.htmlwww/home.html@www/index.html151.12 Comparison: FTP passive vs active mode
| Point | Passive (ftp:passive-mode on, default) | Active (ftp:passive-mode off) |
|---|---|---|
| Command | PASV -> 227 (192,168,171,20,117,58) | PORT 192,168,171,10,132,183 |
| Data port | 117 x 256 + 58 = 30010 on the server | 132 x 256 + 183 = 33975 on the client |
| Who opens data | Client -> server | Server (from port 20) -> client |
| Behind NAT | Works | Blocked by the client's NAT/firewall |
- net:max-retries 2 on a closed port: lftp reconnected twice and then gave a fatal error.
- xfer:clobber is off by default, so get refuses to overwrite: 'file already exists and xfer:clobber is unset'. Turn it on to replace.
151.13 Common mistakes
- Expecting mirror -R to remove server files you deleted locally: it only does that with --delete.
- Thinking mirror sends only the changed part like rsync: a changed file is removed and sent again in full.
- Using chmod 644 *.html over FTP: the wildcard is not expanded, write the names.
- Saving passwords in bookmarks (bmk:save-passwords yes).
- Scripts without cmd:fail-exit yes: a failed cd and the script keeps uploading to the wrong place.
151.14 Interview tips
- Deploy answer: mirror -R --dry-run --delete first, then --only-newer with --exclude-glob, cmd:fail-exit on, and SFTP instead of FTP.
- Resume: put -c / reput for uploads, mirror --continue for a partial file.
- Passive mode is default because the client opens the data connection, so client-side NAT is not a problem.
- set -a shows all settings, set -d the defaults; a closure like /srv limits a setting to one host.