If you need to use FTP to upload some files to a server every so often and want to save yourself some time, you can make a simple Bash script to transfer the files quickly. Rather than entering the username, password, and directory manually, we can get our Bash script to do this tedious legwork for us. In this tutorial, you will see an example script to make FTP transfers a cinch on a Linux system.
In this tutorial you will learn:
- Example of simple bash script ftp client
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Software||Bash shell, ftp|
|Other||Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
Example of simple bash script ftp client
This script first defines variables such as hostname of the ftp server, username and password and then it creates an ftp session and uploads the specified file into your selected directory:
#!/bin/bash ftp_site=127.0.0.1 username=ftpuser passwd=pass PS3='Select a destination directory: ' # bash select select path in "." "/test" "public_html/myblog/" "backup/images/" do ftp -n $ftp_site<<EOF quote USER $username quote PASS $passwd binary cd $path put $1 quit EOF break done
Be sure to edit the
passwd variables above. You should also change the paths to whichever directories you most commonly upload your files to.
Executing the script:
$ chmod +x ftp_bash_script.sh $ ./ftp_bash_script.sh file1
Example script output:
$ ./ftp_bash_script.sh somerandomfile 1) . 2) /test 3) public_html/myblog/ 4) backup/images/ Select a destination directory: 2 Connected to 127.0.0.1. 220 (vsFTPd 3.0.5) 331 Please specify the password. 230 Login successful. 200 Switching to Binary mode. 250 Directory successfully changed. local: somerandomfile remote: somerandomfile 229 Entering Extended Passive Mode (|||10078|) 150 Ok to send data. 0 0.00 KiB/s 226 Transfer complete. 221 Goodbye.
In this tutorial, we saw how to create an FTP Bash script on a Linux system. Rather than painstakingly entering our hostname, username, and password each time, we can get our script to do the repetitive work for us. Be sure to edit the variables to best suit your uploading habits, including all of the directories that you commonly upload your files to.