Skip to content
Related Articles

Related Articles

Shell Script to Download Files From a Source Directory on a Remote FTP Server

View Discussion
Improve Article
Save Article
  • Last Updated : 20 Apr, 2021

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 download a file directly using commands, then we will extend it to a script.

The FTP commands for downloading files are “get” and “mget” which are used for downloading single or multiple files respectively. These commands should be entered inside an FTP prompt. Files are always downloaded from the “current directory” on the server. There is no way to specify which directory to download from, so we first have to change to the required source directory before downloading.

A list of commands useful for downloading files (Examples are shown in the problem below):

1. get filename         #download file
2. mget file1 file2     #download multiple files
3. cd dirname              #change remote directory
4. lcd dirname          #change local directory
5. ftp hostname          #login to ftp server
6. bye                    #terminate ftp connection and quit ftp

Consider the problem below to understand downloading files using FTP:

Download 2 files “f1.txt” and “program1.c” from the src directory inside the server to dst directory inside the local computer.

Shell Script to download the files from a source directory on a remote FTP server

Current Server and Local Directory Structure

Stepwise Implementation for Downloading:

Step 1: First, login to ftp using “ftp hostname” command and enter login details.

Shell Script to download the files from a source directory on a remote FTP server

Step1: Login to FTP

Step 2: Change to src directory on server using “cd src” and also change to dst directory on local computer using “lcd dst”.

Step2: Change to required directories on both server and local computer

Step 3: Now type “mget file1.txt program1.c” to download the files. Then type “bye” to terminate FTP connection and exit FTP prompt.

Step3: Download files and exit ftp

A shell script to perform downloads in Stepwise:

Step 1: First, store FTP login details in variables inside the script. These values don’t change, so you don’t need to ask the user every time, and it makes logging into the server convenient.

HOST = ""    #Server's hostname
USER = "lapowner"            #Server login username
PASSWORD = "1234asdf@Z"    #Server login password

Step 2: Take the source directory and a list of files to download as input through arguments to the script.

SOURCE = $1        
#$1 is the first argument to the script

#${@:2} is the list of arguments without the first one

Step 3: Write FTP command with parameters -inv or -i -n -v.

The parameters are described below:

-iDisable interactive mode in ftp. By using this, ftp will not ask for confirmation for every file being downloaded.
-nHelps to log in manually to ftp using “user $USERNAME $PASSWORD”. Without, this ftp would assume your local desktop’s username as the server’s username.
-vVerbose mode for ftp. This makes the output more detailed.

Also, Supply FTP commands(like mget etc) to FTP through input redirection using “<<“. Use “EOF” to mark the beginning and end of the input to FTP.

ftp -inv $HOST <<EOF #Begin input to FTP
EOF  #End input to FTP

Step 4: Run the script with the arguments i) the source directory and ii) a list of files:

./ file1     
#for downloading file1

./ *.c file1 
#for downloading file1 and all c files

Complete Shell script to download files from a source directory on a remote FTP server:


HOST = ""
USER = "lapowner"
PASSWORD = "1234asdf@Z"

ALL_FILES = "${@:2}"

ftp -inv $HOST <<EOF


Shell Script to download the files from a source directory on a remote FTP server

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!