Shell Script to Download Files From a Source Directory on a Remote FTP Server
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.
Current Server and Local Directory StructureStepwise Implementation for Downloading:
Step 1: First, login to ftp using "ftp hostname" command and enter login details.
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 ftpA 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 = "192.168.0.104" #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
ALL_FILES="${@:2}"
#${@: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:
Parameter | Description |
---|
-i | Disable interactive mode in ftp. By using this, ftp will not ask for confirmation for every file being downloaded. |
-n | Helps 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. |
-v | Verbose 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
user $USER $PASSWORD
cd $SOURCE
mget $ALL_FILES
bye
EOF #End input to FTP
Step 4: Run the script with the arguments i) the source directory and ii) a list of files:
./script.sh file1
#for downloading file1
./script.sh *.c file1
#for downloading file1 and all c files
Complete Shell script to download files from a source directory on a remote FTP server:
#!/bin/bash
HOST = "192.168.0.104"
USER = "lapowner"
PASSWORD = "1234asdf@Z"
SOURCE = $1
ALL_FILES = "${@:2}"
ftp -inv $HOST <<EOF
user $USER $PASSWORD
cd $SOURCE
mget $ALL_FILES
bye
EOF
Output:
Similar Reads
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
How To Download Single Folder or Directory From GitHub Repository?
GitHub is a popular platform for hosting and collaborating on code repositories. Often, you might come across a situation where you need to download just a single folder or directory from a GitHub repository rather than cloning the entire repository. This can save time and bandwidth, especially when
3 min read
Shell Script to Put File in a FTP Server
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 dir
4 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 files from an external server with code in PHP ?
PHP provides many inbuilt variables or functions to perform this kind of operation. One of them is file_get_contents to download the files from the external server using PHP.file_get_contents() Function Parameters:$path: It declares the path of the file which we are going to fetch.$include_path: Thi
2 min read
How to List all Files and Directories in FTP Server using Python?
FTP ( File Transfer Protocol ) is set of rules that computer follows to transfer files across computer network. It is TCP/IP based protocol. FTP lets clients share files. FTP is less secure because of files are shared as plain text without any encryption across the network. It is possible using pyt
2 min read
bash: cd: Downloads: No such file or directory in Linux
Encountering errors while navigating through the command line interface can be frustrating, especially for beginners. One common error, "Bash: Cd: Downloads: No Such File Or Directory," often occurs when users attempt to change directories to a location that doesn't exist or isn't accessible. Howeve
4 min read
How to Download and Upload Files in FTP Server using Python?
Prerequisite: FTP, ftplib Here, we will learn how to Download and Upload Files in FTP Server Using Python. Before we get started, first we will understand what is FTP. FTP(File Transfer Protocol) File Transfer Protocol(FTP) is an application layer protocol that moves files between local and remote f
3 min read
Pymeta - Search The Web For Files On A Domain To Download And Extract Metadata
PyMeta tool is an automated cyber-security tool which is developed in the Python language that has the potential to search for queries, identify and get the following file types (pdf, xls, xlsx, csv, doc, docx, ppt, pptx) from a given target domain using Google and Bing scraping engines. PyMeta tool
2 min read
Download Any File From URL with Vanilla JavaScript
Downloading files from a URL using vanilla JavaScript involves creating a link element, setting its href attribute to the file URL, and programmatically triggering a click event. This method allows users to download files without relying on external libraries or frameworks. Downloading files from a
3 min read