FTP Commands

File Transfer Protocol commands for Bash scripting

📁 What is FTP?

FTP (File Transfer Protocol) allows you to transfer files between computers over a network. Using FTP commands in Bash scripts, you can automate file uploads, downloads, and remote file management tasks efficiently and reliably.


# Basic FTP connection
ftp ftp.example.com

# Or use in script
ftp -n ftp.example.com << EOF
user username password
bye
EOF
                                    

Output:

Connected to ftp.example.com

221 Goodbye.

Common FTP Commands

🔐

user

Login with credentials

user username password
⬆️

put

Upload files to server

put localfile.txt
⬇️

get

Download files from server

get remotefile.txt
📋

ls

List remote directory

ls -la

🔹 Basic FTP Connection

Establish FTP server connections using command-line clients for file transfer operations and remote directory management. Connect interactively with ftp ftp.example.com or use ftp -n ftp.example.com for scripted authentication. After connection, authenticate with user username password, navigate directories using cd and ls, and transfer files with get and put commands. Set transfer modes with ascii for text files and binary for all other file types to ensure data integrity during transfers.

#!/bin/bash

# FTP server details
FTP_HOST="ftp.example.com"
FTP_USER="myusername"
FTP_PASS="mypassword"

# Connect and login
ftp -n $FTP_HOST << EOF
user $FTP_USER $FTP_PASS
pwd
ls
bye
EOF

echo "FTP session completed"

Output:

Connected to ftp.example.com

230 User logged in

257 "/home/user" is current directory

drwxr-xr-x 2 user group 4096 Jan 01 12:00 documents

-rw-r--r-- 1 user group 1024 Jan 01 12:00 file.txt

221 Goodbye

FTP session completed

🔹 Uploading Files

Transfer files from local systems to remote FTP servers using upload commands with proper mode configuration for data integrity. Use put localfile.txt for single file uploads or mput *.txt for multiple files matching patterns. Always set appropriate transfer mode with binary for images, archives, and documents, or ascii for plain text files. Verify successful transfers through confirmation messages and file size validation. These upload techniques enable efficient content deployment, backup operations, and remote file management through standardized FTP protocols.

#!/bin/bash

FTP_HOST="ftp.example.com"
FTP_USER="username"
FTP_PASS="password"
LOCAL_FILE="report.pdf"

# Upload single file
ftp -n $FTP_HOST << EOF
user $FTP_USER $FTP_PASS
binary
cd /uploads
put $LOCAL_FILE
bye
EOF

echo "File uploaded successfully"

# Upload multiple files
ftp -n $FTP_HOST << EOF
user $FTP_USER $FTP_PASS
binary
cd /uploads
mput *.txt
bye
EOF

echo "Multiple files uploaded"

Output:

Connected to ftp.example.com

230 User logged in

200 Type set to I (binary)

250 CWD command successful

226 Transfer complete

File uploaded successfully

mput file1.txt? y

mput file2.txt? y

Multiple files uploaded

🔹 Downloading Files

Efficiently retrieve files from remote servers using curl's versatile download options for individual and batch operations. Use curl -O https://example.com/file.zip to save with the original filename, or curl -o customname.zip https://example.com/file.zip to specify a custom name. For resuming interrupted downloads, employ -C -, and monitor progress with --progress-bar. Multiple simultaneous downloads can be achieved through scripting or parallel execution. These download techniques ensure reliable file retrieval for software distribution, media acquisition, and backup operations.

#!/bin/bash

FTP_HOST="ftp.example.com"
FTP_USER="username"
FTP_PASS="password"
REMOTE_FILE="data.csv"

# Download single file
ftp -n $FTP_HOST << EOF
user $FTP_USER $FTP_PASS
binary
cd /downloads
get $REMOTE_FILE
bye
EOF

if [ -f "$REMOTE_FILE" ]; then
    echo "File downloaded: $REMOTE_FILE"
else
    echo "Download failed"
fi

# Download multiple files
ftp -n $FTP_HOST << EOF
user $FTP_USER $FTP_PASS
binary
cd /downloads
prompt off
mget *.log
bye
EOF

echo "All log files downloaded"

Output:

Connected to ftp.example.com

230 User logged in

200 Type set to I (binary)

250 CWD command successful

226 Transfer complete (1024 bytes)

File downloaded: data.csv

Receiving app.log (2048 bytes)

Receiving error.log (512 bytes)

All log files downloaded

🔹 Directory Operations

Navigate remote directories using cd, create new directories with mkdir, and remove them with rmdir. Use pwd to check your current location and ls to list contents. These commands help organize files on the remote server.

#!/bin/bash

FTP_HOST="ftp.example.com"
FTP_USER="username"
FTP_PASS="password"

# Directory operations
ftp -n $FTP_HOST << EOF
user $FTP_USER $FTP_PASS

# Show current directory
pwd

# List files
ls -la

# Create new directory
mkdir backup_$(date +%Y%m%d)

# Change to directory
cd backup_$(date +%Y%m%d)

# Verify location
pwd

# Go back to parent
cd ..

bye
EOF

echo "Directory operations completed"

Output:

Connected to ftp.example.com

230 User logged in

257 "/home/user" is current directory

drwxr-xr-x 2 user group 4096 Jan 01 12:00 documents

257 "backup_20250110" directory created

250 CWD command successful

257 "/home/user/backup_20250110" is current directory

250 CWD command successful

Directory operations completed

🔹 Deleting Remote Files

Remove files from the remote server using the delete command for single files or mdelete for multiple files. Always verify file names before deletion to avoid removing important data. Use with caution in production scripts.

#!/bin/bash

FTP_HOST="ftp.example.com"
FTP_USER="username"
FTP_PASS="password"

# Delete files
ftp -n $FTP_HOST << EOF
user $FTP_USER $FTP_PASS
cd /temp

# Delete single file
delete old_file.txt

# Delete multiple files (with confirmation off)
prompt off
mdelete temp_*.log

# Remove empty directory
rmdir old_backup

bye
EOF

echo "Cleanup completed"

Output:

Connected to ftp.example.com

230 User logged in

250 CWD command successful

250 DELE command successful

Deleting temp_app.log

Deleting temp_error.log

250 RMD command successful

Cleanup completed

🔹 Automated FTP Backup Script

Create a complete backup script that connects to FTP, uploads files, and logs the operation. This practical example shows error handling, logging, and proper FTP session management for production use. Schedule it with cron for automatic backups.

#!/bin/bash

# Configuration
FTP_HOST="ftp.backup.com"
FTP_USER="backup_user"
FTP_PASS="secure_password"
BACKUP_DIR="/var/backups"
REMOTE_DIR="/backups/$(date +%Y/%m)"
LOG_FILE="/var/log/ftp_backup.log"

# Create backup archive
BACKUP_FILE="backup_$(date +%Y%m%d_%H%M%S).tar.gz"
tar -czf "/tmp/$BACKUP_FILE" $BACKUP_DIR

# Upload to FTP
echo "$(date): Starting FTP upload" >> $LOG_FILE

ftp -n $FTP_HOST << EOF >> $LOG_FILE 2>&1
user $FTP_USER $FTP_PASS
binary
mkdir -p $REMOTE_DIR
cd $REMOTE_DIR
put /tmp/$BACKUP_FILE
bye
EOF

if [ $? -eq 0 ]; then
    echo "$(date): Backup uploaded successfully" >> $LOG_FILE
    rm -f "/tmp/$BACKUP_FILE"
else
    echo "$(date): Backup upload failed" >> $LOG_FILE
    exit 1
fi

echo "Backup completed: $BACKUP_FILE"

Output:

Connected to ftp.backup.com

230 User logged in

200 Type set to I (binary)

257 Directory created

226 Transfer complete (15728640 bytes)

Backup completed: backup_20250110_143022.tar.gz

🧠 Test Your Knowledge

Which FTP command is used to upload a file?