Shell Script to Put File in a FTP Server
Last Updated :
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.
Basic FTP Command:
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:

Shell Script to put the file in an FTP server:
#!/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 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:
Option | Meaning |
---|
-i | Disable interactive mode, so that FTP will not ask for confirmation of each file while using mput command etc. We are using this for convenience while uploading or downloading files |
-n | Disable auto-login. We have to do this, so we can manually log in using "user" command inside the script |
-v | Enables verbose mode. This helps us to see the server responses after executing each FTP command |
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):
Similar Reads
Shell script to send email
In today's fast-paced digital world, email remains one of the most essential communication tools. Whether it's for business correspondence, personal communication, or automated notifications, sending emails efficiently is crucial. Shell scripting offers a powerful way to automate various tasks, incl
4 min read
How to Create a Shell Script in linux
Shell is an interface of the operating system. It accepts commands from users and interprets them to the operating system. If you want to run a bunch of commands together, you can do so by creating a shell script. Shell scripts are very useful if you need to do a task routinely, like taking a backup
7 min read
How to Run a Shell Script in Linux
Shell scripts are a powerful way to automate tasks and manage system processes in Linux. These scripts, written in shell programming languages like Bash, allow users to execute a sequence of commands efficiently. In this guide, we'll show the steps to check a shell script in Linux before running it,
4 min read
Shell Script to Download Files From a Source Directory on a Remote FTP Server
In this article, we are going to write a shell script to download files from a certain directory in a server to a local computer using FTP. It is recommended to have some knowledge of FTP before going through this article, but it is not necessary. Before writing a shell script, we will see how to do
3 min read
Introduction to Linux Shell and Shell Scripting
If we are using any major operating system, we are indirectly interacting with the shell. While running Ubuntu, Linux Mint, or any other Linux distribution, we are interacting with the shell by using the terminal. In this article we will discuss Linux shells and shell scripting so before understandi
8 min read
How to setup and configure an FTP server in Linux?
FTP (file transfer protocol) is an internet protocol that is used for transferring files between client and server over the internet or a computer network. It is similar to other internet protocols like SMTP, which is used for emails, and HTTP, which is used for websites. FTP server enables the func
13 min read
How to setup and configure an FTP server in Linux?
FTP (file transfer protocol) is an internet protocol that is used for transferring files between client and server over the internet or a computer network. It is similar to other internet protocols like SMTP which is used for emails and HTTP which is used for websites. FTP server enables the functio
9 min read
Linux Shell Script to Backup Files and Directory
Backing up our important files and directories is crucial to safeguarding our data. In this article, weâll create a simple shell script that uses the tar utility to create an archive file for backup purposes that we can use whenever we require. The shell script weâll build allows us to specify which
5 min read
How to Download a File from a Server with SSH / SCP?
Secure Copy Protocol (SCP) is a secure file transfer protocol that allows you to transfer files between computers over a secure connection. It is based on the Secure Shell (SSH) protocol and is commonly used to transfer files between servers and local computers. One of the main benefits of using SCP
3 min read
What is File server?
Before learning about File Servers, we need to understand why it is introduced into computer networks. Different users within a network need file storage and file-sharing solution which is accessible from any location. So they upload their files to a server and share them with their companion on a n
4 min read