Bash | Tutorials

David Carr

Bash loop through files and FTP them

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.


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 to run this:



Domains are often purchased from multiple providers, keeping track of where a domain is and its DNS settings can be tricky. Domain Mapper solves this by listing all your domains in one place. View your DNS settings and receive reminders to renew your domains. Try it today.

Support my work by donating with PayPal.

Subscribe to my newsletter

Subscribe and get my books and product announcements.

© 2009 - 2021 DC Blog. All code MIT license. All rights reserved.