Very often I need to upload some files to my web server from a command line. entering a user name and password, changing directory can be little tedious work. Here is a small bash script which make this work easier. This script first defines a variables such as hostname of the ftp server, username and password and then it creates ftp session and uploads file into your selected directory:

#!/bin/bash

ftp_site=myhostname
username=myusername
passwd=mypass

PS3='Select a destination directory: '

# bash select
select path in".""public_html/""public_html/myblog/""backup/images/"
do
ftp -in <<EOF
open $ftp_site
user $username$passwd
cd $path
put $1
close 
bye
EOF
echo$1 uploaded to $path ! 

# Break, otherwise endless loop
break
done

EXECUTE:

$ chmod +x ftp_bash_script.sh
$ ./ftp_bash_script.sh file1

OUTPUT:

1) .                    3) public_html/myblog/
2) public_html/         4) backup/images/
Select a destination directory: 2
file1 uploaded to public_html/ !


Free Linux eBooks

Do you have the right skills?

Our IT Skills Watch page reflects an up to date IT skills demand leaning towards the Linux and Unix environment. We have considered a number of skills and operating systems.

See the result...

Linux Online Training

Learn to run Linux servers and prepare for LPI certification with Linux Academy. 104 available video lessons with PDF course notes with your own server!

Go to top