Bash, Tutorials

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:



David Carr

David Carr

For the past 12 years, I’ve been developing applications for the web using mostly PHP. I do this for a living and love what I do as every day there is something new and exciting to learn.

In my spare time, the web development community is a big part of my life. Whether managing online programming groups and blogs or attending a conference, I find keeping involved helps me stay up to date. This is also my chance to give back to the community that helped me get started, a place I am proud to be apart of.

Besides programming I love spending time with friends and family and can often be found together going out catching the latest movie, staying in playing games on the sofa or planning a trip to someplace I’ve never been before.