Bash loop through files and FTP them

David Carr

2 min read - 6th Apr, 2016

I have a folder of csv files that need uploading to a server I don’t want to do it manually, Bash can handle this task perfectly.

This script will connect to an FTP server then loop through the csv files or any type specified and upload them. You cannot do a loop whilst inside of heredoc block like most examples do, instead the commands are written to a txt file.

First set of variables are the ftp connection details and the type of file to look for, only needed when looking for certain types of files in this case .csv files.

Each command ends in » ftp.txt this write to the file called ftp.txt if the file does not exist it’s created.

Once connected do a cd (change directory) do go to the specified directory.

To loop through the files a ‘for filename in‘ is used pass in the type variable the * is a wildcard to select all matching.

to execute the code stored inside the txt file use ftp < ftp.txt use the -in flag to stop the prompt for a password.

Lastly delete the ftp.txt file.

#!/bin/bash
HOST='ftp.domain.co.uk'
USER='username'
PASSWD='password'
TYPE='.csv'

echo open $HOST >> ftp.txt
echo ascii >> ftp.txt
echo user $USER $PASSWD >> ftp.txt
echo cd /public_html/temp/ >> ftp.txt

for filename in *$TYPE; do
    echo "put $filename"
done >> ftp.txt

echo bye >> ftp.txt
ftp -in < ftp.txt
rm ftp.txt

Save this script as upload.sh to run this:

sh upload.sh

 

0 comments
Add a comment

Copyright © 2024 DC Blog - All rights reserved.