Fetch and Download Files from FTP Location to Linux Box

Fetch File Names & Download Files from FTP Location to Linux Box

Source data files may exists/uploaded in FTP location. We need to know file names of those and also need to download those files to local Linux box. Because we want to extract those files and stage in relational database for data-warehouse. It will be the part of ETL process. If we just want to get file names which are uploaded to FTP location, we have to create a bash function. Inside that function we will create an FTP interaction session.

1 function  get_ftp_file_name_list(){
2    local host_name_with_port="${1}";
3    local ftp_source_path="${2}"
4   local ftp_user="${3}"
5    local ftp_password="${4}"
6 
7   ftp -inv $host_name_with_port << ! 1>/dev/null
8       user $ftp_user $ftp_password  
9       cd "${ftp_source_path}" 
10       ls -1 "all_file_names.txt" 
11       close     
12 !
13 }
14 #Calling the function: get_ftp_file_name_list “ftp://test.com:21”  “datafiles/feeds/”  “testuser”  “***”
Code language: PHP (php)

The function “get_ftp_file_name_list” expect 4 arguments:

SL#Argument NameDescription
1host_name_with_portHost name with port number
2ftp_source_pathFTP source file directory path
3ftp_userValid FTP User Id
4ftp_passwordValid FTP User Password

first “ftp” command create an interactive session:

1 ftp -inv $host_name_with_port << ! 1>/dev/null
2 !Code language: JavaScript (javascript)

It uses 3 options “–inv”.

SL#Argument NameDescription
1-iTurn off interactive prompting. 
2-nFirst time attempting for auto-login. 
3-vEnable verbose message.
user $ftp_user $ftp_password “/dev/null” means system message should not display or redirect to the null.

login to the ftp site with provided userid and password

1 cd "${ftp_source_path}"Code language: JavaScript (javascript)

Change current directory to the provided directory.

1 ls -1 "all_file_names.txt"Code language: JavaScript (javascript)

Fetch only file names from the current directory and result is logged in the all_file_names.txt file.

1 close

will be closed ftp interaction session.

Download Files: If we want to download files from FTP location Linux has a built in command “wget”. Using that we can download files from FTP location.  This command is actually non-interactive downloader from the web. Syntax:

1 wget -nd -nv -np -q -P "${dist_file_path}" --ftp-user="${ftp_user}" --ftp-password="${ftp_password}" <br>--timeout 0 "${ftp_full_file_path}"
Code language: JavaScript (javascript)

“wget” command expect few arguments:

SL#Argument NameDescription
1-ndTurn off hierarchy of directory creation.
2-nvTurn off verbose log needed.
3-qAfter completion it will be quit.
4-PDirectory path where all files will be stored.
5–ftp-userName of the FTP user
6–ftp-passwordPassword
7–timeoutMaximum time the process will be wait for completion.
8“${ftp_full_file_path}”The FTP source path from where file will be downloaded.

SFTP location for File upload/store is more secured then FTP location. Now a days most of the clients store there data files to the SFTP location. I have written another blog for download files from SFTP location. URL:

Fetch File Names from SFTP Location using Shell (bash) Programming

Add a Comment

Your email address will not be published. Required fields are marked *