lftp: file tranfers with a very smart tool!

lftp is a very nice tool that can simplify file transfer operations that would be complicate to do with shell scripting

lftp: file tranfers with a very smart tool!
Photo by Viktor Talashuk on Unsplash

lftp is a very nice tool that can simplify file transfer operations that would be complicate to do with shell scripting

lftp generic syntax

  • protocol: can be ftp,sftp,http,https
  • username: the ftp username, needs to have access to the directories you want to get/put files.
  • password: if the password contains characters like @,! that the shell might try to parse them try to escape them like this \! \@
  • host: the server
  • command: can be common ftp commands like ls mkdir etc, but can be lfpt specific like mirror that we will see some examples, commands must be semicollon separated.$ lftp protocol://username:password@host -e "command1;command2"

How to mirror an sftp directory to localhost (get)

  • mirror: instructs lftp to copy the files from the ftp server to localhost
  • source: is a directory on the ftp host
  • target: is a directory on local host

One thing you have to be aware is that you need to run lftp as a user which have access rights to the target directory or else copy will fail.$ lftp sftp://username:password@host -e "mirror source target"

How to mirror a local directory to the sftp server (put)

  • mirror -R: instructs lftp to copy files from localhost to the ftp server
  • source: is a directory on local host
  • target: is a directory on ftp host

One thing you have to be aware is that you need to run lftp as a user which have access rights to the source directory or else copy will fail.$ lftp sftp://username:password@host -e "mirror -R source target"

Cool options that make lftp cool

  • mirror -P n: Copy files in parallel, n is the number of files
  • mirror -c: Resume a copy operation if possible

Here is a complete list of options as taken from the man page-c, --continue      continue a mirror job if possible
           -e, --delete        delete files not present at remote site
               --delete-first       delete old files before transferring new ones
               --depth-first        descend into subdirectories before transferring files
           -s, --allow-suid         set suid/sgid bits according to remote site
               --allow-chown   try to set owner and group on files
               --ascii         use ascii mode transfers (implies --ignore-size)
               --ignore-time        ignore time when deciding whether to download
               --ignore-size        ignore size when deciding whether to download
               --only-missing  download only missing files
               --only-existing download only files already existing at target
           -n, --only-newer    download only newer files (-c won't work)
               --no-empty-dirs don't create empty directories (implies --depth-first)
           -r, --no-recursion  don't go to subdirectories
               --no-symlinks   don't create symbolic links
           -p, --no-perms      don't set file permissions
               --no-umask      don't apply umask to file modes
           -R, --reverse       reverse mirror (put files)
           -L, --dereference   download symbolic links as files
           -N, --newer-than=SPEC    download only files newer than specified time
               --on-change=CMD      execute the command if anything has been changed
               --older-than=SPEC    download only files older than specified time
               --size-range=RANGE   download only files with size in specified range
           -P, --parallel[=N]  download N files in parallel
               --use-pget[-n=N]     use pget to transfer every single file
               --loop          loop until no changes found
           -i RX, --include RX include matching files
           -x RX, --exclude RX exclude matching files
           -I GP, --include-glob GP include matching files
           -X GP, --exclude-glob GP exclude matching files
           -v, --verbose[=level]    verbose operation
               --log=FILE      write lftp commands being executed to FILE
               --script=FILE        write lftp commands to FILE, but don't execute them
               --just-print, --dry-run   same as --script=-
               --use-cache          use cached directory listings
           --Remove-source-files    remove files after transfer (use with caution)
           -a             same as --allow-chown --allow-suid --no-umask

Enjoy your file transfers!

Join Medium with my referral link - Konstantinos Patronas
As a Medium member, a portion of your membership fee goes to writers you read, and you get full access to every story…