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/ !


Go to top