Shell Script to Put File in a FTP Server

09 Apr, 2021
The File Transfer Protocol also called FTP is used to transfer files from client to server and vice-versa. It mainly uses port 21 for communication.
Here we can simplify the process of uploading files using FTP. But, before we write a script, let’s look at how to get/put files onto an ftp server directly using commands.
The commands to start an FTP communication and end it is shown below:
ftp server #Prompts for login details and connects to server
#Example: 
ftp 192.168.0.104

bye           #Terminates the ftp connection and exits ftp
Example:
After typing “FTP hostname”, you will be asked for a username and password. If login is successful after entering the details, we start in the FTP user’s home directory on the server. Any file you upload now gets uploaded to this directory. If you have to upload a file in some other directory on the server, you first have to change to that directory using the “cd” command. Note that, using the cd command in an FTP prompt only changes directory on the server, i.e. we will still be in the same directory on our local computer.
Commands in FTP help us to navigate the server’s directories, fetch and upload files from and to the server. To get single or multiple files, we can use commands “get” and “mget” respectively. Similarly, to put single or multiple files, we can use commands “put” and “mput” respectively.
Some important ftp commands:
ls                   #Lists files in server
cd dir                #Change directory in server
get file1.c           #Downloads file1.c
put file.txt       #Uploads file.txt
mput *.c file.txt  #Uploads all c files and file.txt
Example of putting a file in server through FTP prompt:
#!/bin/bash

# The 3 variables below store server and login details
HOST="192.168.0.104"
USER="user1"
PASSWORD="1234"


# $1 is the first argument to the script
# We are using it as upload directory path
# If it is '.', file is uploaded to current directory.
DESTINATION=$1


# Rest of the arguments are a list of files to be uploaded.
# ${@:2} is an array of arguments without first one.
ALL_FILES="${@:2}"


# FTP login and upload is explained in paragraph below
ftp -inv $HOST <<EOF
user $USER $PASSWORD
cd $DESTINATION
mput $ALL_FILES
bye
EOF
The above script requires the following data:
Server’s hostnameServer user’s login detailsThe directory in which to upload files on the server (passed as an argument to the script)The list of files to be uploaded to the server (passed as an argument to script)
Server’s hostname
Server user’s login details
The directory in which to upload files on the server (passed as an argument to the script)
The list of files to be uploaded to the server (passed as an argument to script)
After logging in to the server, we need to enter FTP commands manually, but by using input redirection we can supply the commands directly in the script. “<<” is used for input redirection and “EOF” is used to mark the beginning and end of the FTP input. 
The “mput” command has been used to upload files as mput can upload either a single file or multiple files.
If login is successful and the files given as input to the script are available, all the files should have been put in the server along with a success message displayed for each file.
The options -inv can also be written as -i -n -v and their functions are explained in the below table:
To execute the script supply the upload directory and also a list of files:
./script_name.sh  path_to_upload file1 file2 file3
File Uploading Example (Put all .c files and f1.txt in the current directory of server):
Picked
Shell Script
Linux-Unix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.