How to Copy Files on Remote Servers Using LFTP (Equivalent to cp Command)


4 views

When working with LFTP, many users notice that while mv works for moving files, there's no direct cp equivalent for copying files on remote servers. This can be confusing for those transitioning from standard Linux commands.

The most straightforward method is to use a combination of get and put commands with different filenames:

lftp user@server.com
cd /remote/directory
get original.txt -o copy_of_original.txt
put copy_of_original.txt

For copying entire directories, the mirror command is more efficient:

mirror --only-newer /source/directory /destination/directory

The --only-newer flag prevents overwriting existing files unless they're newer.

When dealing with large files, consider using parallel get:

pget -n 4 bigfile.iso -o bigfile_copy.iso

The -n 4 parameter splits the download into 4 segments for faster transfer.

For batch operations, you can use LFTP's scripting capabilities:

lftp -c "open -u user,password ftp.example.com;
         cd /source;
         get file1.txt -o file1_copy.txt;
         get file2.txt -o file2_copy.txt;
         put file1_copy.txt;
         put file2_copy.txt"

Remember that these operations require sufficient permissions on the remote server. Also, be mindful of storage space when creating copies of large files.


LFTP is a powerful command-line file transfer program that supports multiple protocols like FTP, SFTP, and HTTP. While moving files with mv is straightforward, copying files requires a different approach since there's no direct cp equivalent in LFTP.

To copy files on a remote server using LFTP, you need to combine the get and put commands. Here's the basic workflow:

lftp -u username,password sftp://example.com
get /remote/path/file.txt -o /tmp/file.txt
put /tmp/file.txt -o /remote/path/newfile.txt

For larger files, you can use LFTP's pipe functionality to avoid writing to local storage:

lftp -u username,password sftp://example.com
cat /remote/path/file.txt | put -o /remote/path/newfile.txt

When you need to copy entire directories, the mirror command is more efficient:

lftp -u username,password sftp://example.com
mirror --only-newer /source/directory /destination/directory

Create a script file (copy_script.lftp) for repeated operations:

open sftp://example.com -u username,password
get /remote/file1.txt -o /tmp/file1.txt
put /tmp/file1.txt -o /remote/file1_copy.txt
bye

Then execute it with:

lftp -f copy_script.lftp

Add these options for better debugging:

set xfer:log true
set xfer:log-file /path/to/logfile.log
set cmd:fail-exit true