Skip to content

lftp | Cheatsheet

lftp is a command-line program client for several file transfer protocols.

lftp is designed for Unix and Unix-like operating systems. It was developed by Alexander Lukyanov, and is distributed under the GNU General Public License

on a professional level, LFTP is becoming a super-reliable option for me when automating FTP transfers with script.

LFTP is a more robust FTP client than just plain FTP or cURL. Unlike those clients, it retries a few times when transmission fails, has mirroring features, and supports simultaneous multi-file transfers, recursion, and Regular-Expression matches. Speed, from what I've experienced, is identical.


Install net-ftp/lftp

emerge --ask net-ftp/lftp

Customize lftp prompt line style

set prompt "\[\e[0;32m\]┌┼───┤\[\e[1;33m\] [lftp] \[\e[0;36m\]\u\[\e[0;33m\]\@\[\e[0;36m\]\h \[\e[0;32m\]├───┤\[\e[0;33m\]\w\[\e[0;32m\]├───\n└┼─\[\e[1;33m\]$\[\e[0;32m\]─┤► :\[\e[0m\] "

Torrent settings

set torrent:ip ""
set torrent:ipv6 ""
set torrent:max-peers 60
set torrent:port-range 6881-6889
set torrent:retracker ""
set torrent:seed-max-time 30d
set torrent:seed-min-peers 3
set torrent:stop-on-ratio 2.0
set torrent:use-dht yes

Add ssh key in either way in lftprc

set ssl:key-file ~/.ssh
set sftp:connect-program "ssh -a -x -i yourprivatekeyfile"

FXP: Server A > Server B

lftp -c 'open -e "mirror <server-A_dir_path> <server-B_url>" <server-A_site>'

Dump current settings

lftp -e "set;exit"

Dump all settings

lftp -e "set -a ;exit"

Multi-segment file downloading with lftp

lftp -u user,pass ftp://site.com -e 'pget -c -n 6 file'

Multi-segment directory downloading with lftp

lftp -u user,pass ftp://site.com/ -e 'mirror -c --parallel=3 --use-pget-n=5 "Some folder"'

Upload a File

lftp -e 'put /local/path/yourfile.mp4; bye' -u user,password ftp.foo.com

Connection Timeout

lftp -e 'set net:timeout 10; put /local/path/yourfile.mp4; bye' -u user,password ftp.foo.com

Active Mode

For what it's worth, Active mode -- older, stinkier, and won't go through your firewall:

lftp -e 'set ftp:passive-mode false; set net:timeout 10; put /local/path/yourfile.mp4; bye' -u user,password ftp.foo.com

Download

lftp -e 'set net:timeout 10; get yourfile.mp4; bye' -u user,password ftp.foo.com

If you need to put it in a specific local directory

lftp -e 'set net:timeout 10; get yourfile.mp4 -o /local/path/yourfile.mp4; bye' -u user,password ftp.foo.com

Recursive Upload

lftp -e 'mirror -R /local/path/ /' -u user,password ftp.foo.com

Recursive Download

lftp -e 'mirror / /local/path/' -u user,password ftp.foo.com

Regular-Expression–Match Upload

lftp -e 'mirror -R -i "\.(html?|css|js)$" /local/path/ /' -u user,password ftp.foo.com

Non-Recursive

Deploy only root-level files, not the entire directory tree. The -r switch disables recursion. In this example, we are also only deploying HTML, CSS, and JavaScript

JavaScript

lftp -e 'mirror -R -r -i "\.(html?|css|js)$" /local/path/ /' -u user,password ftp.foo.com

Special Characters in Username or Password

lftp -e 'put /local/path/yourfile.mp4; bye' -u user,password\!\! ftp.foo.com

FXP Transfer: site 1 > site2

lftp -d  -c open -e "mirror  -P15 \
    "ftp://user:password@host:port/path" \
    'ftp://user:password@host:port/path'

FXP Transfer: site 1 > site2

lftp -e "mirror -I googleAnalytics-master-dfd9155.tar.gz \ 
    https://extdist.wmflabs.org/dist/extensions ftp://wuseman:odemnn@localhost:65005/site/"

Download all files from server in 20 parallel transfers

lftp -d -e open https://extdist.wmflabs.org/dist/extensions/ -e "mirror -P 20 ."

Disable notification check

mkdir ~/.lftp
echo "set ssl:verify-certificate no" >> ~/.lftp/rc

Download all folders inside /mp3/* on ftp to mp3

lftp -e open ftp://user1:pass1:site1:port -e "mirror -c -P1 remote/path /local/path" -d

FXP Between SITE 1 and SITE 2 with 20 threads at same time

lftp -e "set ftp:use-fxp true ;mirror -R -P20 ftp://user1:pass1:site1:port:/path \
    ftp://user1:pass1:site1:port:/path"  -d

Use lftp to multi-threaded download files from websites

lftp -c "pget -n 10 http://example.com/foo.bar"

Mirrors directory to a ftp server

lftp -u login,passwd" -e "mirror reverse /my/from/dir/ /ftp/target/dir/" ftp://site.com/

Mirror a directory structure from websites with an Apache-generated file indexes

lftp -e "mirror -c" http://example.com/foobar/

Multi-segment file downloading with lftp

lftp -u "user,pass" ftp://site.com -e 'pget -c -n 6 file'

Mirror a directory structure from websites with an Apache-generated file indexes

lftp -e "mirror -c" http://example.com/foobar/

Backup sda partition to ftp ( using pipes and gziped backup )

dd if=/dev/sda bs=2048 conv=noerror,sync | gzip -fc | lftp -u user,passwd domain.tld -e "put /dev/stdin -o backup-$(date +%Y%m%d%H%M).gz; quit"

Fastest segmented parallel sync of a remote directory over ssh

lftp -u user,pwd -e "set sftp:connect-program 'ssh -a -x -T -c arcfour -o Compression=no'; mirror -v -c --loop --use-pget-n=3 -P 2 /remote/dir/ /local/dir/; quit" sftp://remotehost:22

Multi-segment directory downloading with lftp

lftp -u "user,pass" ftp://site.com/ -e 'mirror -c parallel=3 use-pget-n=5 "Some folder"'

Gets directory and files tree listing from a FTP-server

lftp -u "user,pass" ftp://site.com/ -e "du -a;exit" > server-listing.txt

!!! Resource(s)

* <http://lftp.yar.ru/lftp-man.html>
* <http://mewbies.com/lftp/lftp.html#scripts>
* <https://web.archive.org/web/20220331040412/http://www.russbrooks.com/2010/11/19/lftp-cheetsheet>
* <https://dade2.net/kb/complete-lftp-command-tutorial/>
* <https://www.nr1.nu/linux-shell/network/lftp/>
* <https://web.archive.org/web/20080309195640/http://tutorials.papamike.ca/pub/lftp.html>
* <https://www.mail-archive.com/lftp@uniyar.ac.ru/>