Upload files to FTP using Bash

This morning, I wanted to automatically update a little website from my computer without using Filezilla. So I searched a little bit and found that there is a very useful tool called nftp. To install it, you only have to use apt-get :

sudo apt-get install ncftp

This tool can be used directly from bash to upload files to FTP (and also to get files of course).

In my case, I just have to put a complete hierarchy of files into a specific folder of my website. So I wrote a little script that put everything on the FTP server :

read -s -p "Enter Password: " mypassword
ncftp <<EOF
open -u username -p $mypassword yoursitehost
cd "folder on the website"
lcd "folder on the computer"
put -R *
bye
EOF

This script asks you your password. With that, you don't have to put it in clear inside your program. The most important command here is the put -R * that make a recursive upload of the current folder in the current remote folder.

If I launch it from my computer (using the good values of course), it gives me that :

$ bash website.sh
NcFTP 3.2.4 (Apr 07, 2010) by Mike Gleason (http://www.NcFTP.com/contact/).
Copyright (c) 1992-2009 by Mike Gleason.
All rights reserved.
Connecting to 74.208.211.161...                                                 
FTP Server ready.
Logging in...                                                                   
User username logged in
Logged in to baptiste-wicht.com.                                                
docs/report.pdf:                                        39.92 kB   79.26 kB/s  
docs/requirements.pdf:                                  40.60 kB   80.91 kB/s  
docs/logbook.pdf:                                       32.39 kB   64.34 kB/s  
docs/week1.pdf:                                         32.13 kB   64.05 kB/s  
docs/01.06.2011.pdf:                                    34.77 kB   69.26 kB/s  
documents.php:                                         869.00 B    6.72 kB/s  
footer.php:                                             99.00 B   45.00 B/s   
header.php:                                            851.00 B  151.56 B/s   
index.php:                                             230.00 B   37.11 B/s   
links.php:                                             188.00 B   37.73 B/s   
minutes.php:                                           254.00 B    1.96 kB/s  
styles/default.css:                                      1.00 kB    8.01 kB/s  
weeks.php:                                             237.00 B   65.24 B/s

You will see the current status of all the files during the upload. The tool is enough smart to detect if a file must be sent or not (so it upload only newer files).

I hope this can help some of you.

Comments

Comments powered by Disqus