The best & Complete lftp command tutorial

lftp command (lftp Manuel Page) is a file transfer program that allows sophisticated FTP, HTTP, and other connections to other hosts. lftp command has a built-in mirror that can download or update a whole directory tree. There is also a reverse mirror (mirror -R) that uploads or updates a directory tree on the server. The mirror can also synchronize directories between two remote servers, using FXP if available.

How to connect to FTP server by lftp? #

This process is very simple and is possible in the following two ways:

Code

lftp HostName (or IP)

then, Type username and password:

lftp ftp.mahdi.biz:~> user mahdi

outputs:

Password: 
lftp [email protected]:~>

How to login to FTP with one-line command: #

also, we can log in with lftp with a one-line command like below:

lftp -u mahdi,passWORDgoeshere example.com

BUT, If your FTP password includes special characters such as $ or ) you’d want to leave off the password like this, then type it in manually when prompted:

lftp -u mahdi example.com

In some cases certain servers will now allow you to connect with lftp with the default settings, you can try the following to connect to these type of servers:

lftp -u userna5,passWORD example.com -e "set ftp:ssl-allow off"

after successfully login :

To see a list of files , type ls command:

lftp ls command result
lftp ls command result

 

To mirror all of the above files in the current directory, enter:

mirror

This command sync data from the remote machine to local. (current directory in local)

and Result :

as you see in the above picture, all remote files are mirrored to the local current directory.

With mirror lftp command you can specify source directory to local target directory. If target directory ends with a slash, the source base name is appended to target directory name. Source and/or target can be URLs pointing to directories. The syntax is:

mirror source target

With mirror lftp command you can specify source directory to local target directory. If target directory ends with a slash, the source base name is appended to target directory name. Source and/or target can be URLs pointing to directories. The syntax is:

lftp [email protected]:~> mirror source target

Code

lftp [email protected]:/> mirror 2000/ /root/data

Result:

I recommend that you use the resume option when using the Mirror command so that the network is interrupted during the file transfer process it will only download partial file.

mirror -c source target

OR :

mirror --continue

To download only newer files (-c won’t work):

mirror --only-newer

OR

mirror -n

You can speed up ftp mirror operation by downloading files in parallel (-P(capital P) or --parallel=N) using the following syntax:

mirror -P

To download 10 files in parallel, enter:

 mirror --parallel=10

You can use pget to transfer every single file:

mirror -c  --use-pget-n=10

How to sync local files to a remote ftp server (upload files) #

You need to pass -R or --reverse option to do reverse mirror i.e. put all files from a local disk to a remote ftp server including sub-dirs and all files in it. In this example reverse mirror /home/mahdi/ to my ftp server :

 

lftp mahdiserver.net lftp [email protected]:~> user mahdi 
Password: 
lftp mahdi@mahdiserver:~>

Change local directory to: /home/mahdi

lftp mahdiserver:~> lcd /home/mahdi/
lcd ok, local cwd=/home/mahdi

To upload (sync):

mirror -R

OR set local directory and remote upload directory as follows:

mirror -R /home/mahdi /new

When using -R, the first directory is local and the second is remote. If the second directory is omitted, base name of first directory is used. If both directories are omitted, current local and remote directories are used. If target directory ends with a slash (except root directory) then base name of source directory is appended.

 

 

How to use lftp Mirror with Exclude Matching Files [ Regex ] #

we want to mirror a directory using FTP and want to exclude certain subdirectories (such as backup) and files ending with *.cvs. with the lftp command, You can exclude files and sub-directories. You can also use glob characters such as *.

lftp command exclude syntax #

The syntax is as follows:

mirror --exclude RX 
mirror --exclude-glob GP

Example:

mirror --exclude backup/

You can use --exclude multiple times:

mirror --exclude logs/ --exclude reports/ --exclude-glob *.bak --exclude-glob *~$

The following will exclude all *.cvs files:

mirror --exclude-glob *.cvs

 

 

List of lftp mirror command options #

-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

Powered by BetterDocs