How to transfer a directory via FTP with Bash

How to transfer a directory via FTP with Bash

In this article, we will see how to transfer a complete directory from a local system to a remote server using FTP in a Bash script.

File Transfer Protocol (FTP) is one of the most common methods for transferring files between computers over a network, especially between clients and servers. Although FTP is a rather old and insecure protocol (as it transmits information in clear text), it is still widely used in various contexts. In this article, we will see how to transfer a complete directory from a local system to a remote server using FTP in a Bash script. To achieve this, we will use both native FTP commands and some more secure and modern alternatives such as lftp.

FTP is a protocol that allows you to transfer files to and from a remote server. However, its basic interface does not directly support recursive copying of directories. To transfer an entire directory, we must first compress it into an archive, transfer the archive, and then unpack it on the remote server.

To transfer a complete directory, it is best to compress it into a .tar.gz or .zip file. Here is an example using tar:


#!/bin/bash

# Variables
HOST="ftp.example.com"
USER="your_user_name"
PASSWORD="your_password"
LOCAL_DIR="/path/to/local/directory"
REMOTE_DIR="/path/to/remote/directory"
ARCHIVE_NAME="backup_directory.tar.gz"

# Compress the directory
tar -czf $ARCHIVE_NAME -C $LOCAL_DIR .

# Open the FTP connection and transfer the file
ftp -inv $HOST << EOF
user $USER $PASSWORD
cd $REMOTE_DIR
put $ARCHIVE_NAME
bye
EOF

# Remove local archive if no longer needed
rm $ARCHIVE_NAME

Code explanation:

  1. Compress directory: The tar -czvf command creates a compressed archive of the specified directory.
  2. Open FTP connection: The ftp -inv command opens an FTP session in batch mode. The options used are:
    • -i: Disable interactive prompts during multiple file transfers.
    • -n: Do not attempt to automatically log in.
    • -v: Verbose mode, to see what happens during the transfer.
  3. Authentication and Transfer: Commands inside the << EOF block are passed to the FTP client. The put command uploads the archive to the remote server.
  4. Removing local archive: After the file has been transferred, the local archive can be deleted.

Although the standard FTP method works, it is more efficient to use an advanced FTP client like lftp to transfer entire directories without having to compress them.

lftp supports recursive transfer of directories, making the process easier. Here is an example script:


#!/bin/bash

# Variables
HOST="ftp.example.com"
USER="your_user_name"
PASSWORD="your_password"
LOCAL_DIR="/path/to/local/directory"
REMOTE_DIR="/path/to/remote/directory"

# Perform recursive transfer
lftp -u $USER,$PASSWORD $HOST << EOF
mirror -R $LOCAL_DIR $REMOTE_DIR
bye
EOF

Code explanation:

  1. mirror command: This command in lftp allows you to transfer entire directories.
    • -R: Indicates that the transfer is from the local system to the remote server (upload). Without this option, the transfer would be from the server to the local (download).
  2. Authentication: This is done via the -u $USER,$PASSWORD command.

FTPS adds a layer of security via SSL/TLS. Here's how to perform a secure FTPS transfer using lftp:


#!/bin/bash

# Variables
HOST="ftp.example.com"
USER="your_username"
PASSWORD="your_password"
LOCAL_DIR="/path/to/local/directory"
REMOTE_DIR="/path/to/remote/directory"

# Perform FTPS transfer recursively
lftp -e "set ftp:ssl-allow yes" -u $USER,$PASSWORD ftps://$HOST << EOF
mirror -R $LOCAL_DIR $REMOTE_DIR
bye
EOF

If you have SSH access to the remote server, SFTP is a much more secure option. Here is an example script with sftp:


#!/bin/bash

# Variables
HOST="example.com"
USER="your_user_name"
LOCAL_DIR="/path/to/local/directory"
REMOTE_DIR="/path/to/remote/directory"

# Perform recursive SFTP transfer
sftp -r $USER@$HOST << EOF
put -r $LOCAL_DIR $REMOTE_DIR
bye
EOF

Code explanation:

  • -r: Option for recursive directory transfer.
  • Security: SFTP uses SSH protocol, so it is encrypted.

Conclusion

In this article, we explored several methods for transferring a directory from a local system to a remote server via FTP using Bash. We saw how to zip a directory and transfer it with standard FTP, how to use the more efficient lftp for recursive transfer, and how to improve security by using FTPS or SFTP. Although FTP is still widely used, it is always better to opt for more secure protocols, such as FTPS or SFTP, to protect the data being transferred.