Skip to main content

Bash Script for FTP

ftp -i -n $FTPSERVER << EOF
user $USER $PASS
mkdir test
cd test
put myfile

But FTP will allow transfer of files only,not the directory tree.
If you want to transfer the Directory structure through FTP you can use LFTP or similar FTP clients. A variety of GUI Based clients are available


lftp has builtin mirror which can download or update a whole directory tree. There is also reverse mirror (mirror -R) which uploads or updates a directory tree on server. Mirror can also synchronize directories between two remote servers, using FXP if available.
It can be downloaded from or

Here is a sample BASH Script to automate the FTP Transfer


Now see how to play with the data we have to upload.That is you can decide whatever folders or files have to be uploaded to the FTP Server.I use two scripts fro this.But we can consolidate it into a single one.

Script 1 -
#echo Where is the Data List
#read LIST

count=`wc -l $LIST | cut -f1 -d" "`

while [ $n -le $count ]
data=`head -$n $LIST | tail -1`
sh /root/scripts/ $data
n=$[$n +1]

Script 2 -

# local directory to pickup

# remote server directory to upload backup

mirror -R $data $BACKUPDIR/
Here the if a deletion takes place at the SOURCE it won't affect the DESTINATION .Means the deleted contents never get deleted from the DESTINATION.
You can optionally delete those files in the DESTINATION also by specifying the --delete switch of the MIRROR command as below
mirror -R -e --delete $LOCALDIR $REMDIR
In some environment with Firewalls,Mix of OSs and FTP Services a few problems may arise in connectivity like SSL Communication,Proxy,etc
Here I have faced an issue with the SSL .By default SSL is enabled in LFTP
After connecting to the FTP Server I just tried to List the contents which turned into errors as below
lftp ftpuser@> ls
'ls' at 0 [FEAT TLS negotiaition..]
'ls' at 0 [ Delaying before Reconnect 29..]
'ls' at 0 [Not Connected..]
lftp ftpuser@>
It repeats
What I did was just disabled SSL
set -a will list all the variables and values for the FTP session
lftp ftpuser@> set -a
SSL was enabled . I turned it to disabled state
lftp ftpuser@> set ftp:ssl-allow no

Thereafter it worked
lftp ftpuser@>ls
12-10-07 11:04PM DIR dir1
12-10-07 11:10PM DIR tesfile.txt
12-07-07 09:48AM DIR TestDir
12-09-07 11:05PM DIR mydata

The same can be applied to the BASH Script also
set ftp:ssl-allow no
To know more about FTP-SSL See RFC2228
FXP Mirroring

server A -> server B
When mirroring is done between two remote servers the File eXchange Protocol is used. Obviously, both servers must support this protocol for this operation to succeed.
Technically, FXP is not a protocol but an extension of FTP. It is used to transfer data from one remote server to another without routing this data through the client. The client sends and receives control data to make everything work.
In an FXP session, the client maintains a standard FTP connection to both servers, and can direct either server to connect to the other to initiate a data transfer. The advantage of using FXP (server A -> server B) instead of (twice using) FTP (server A -> client -> server B) is evident when both servers are high-bandwidth but the client is low-bandwidth.
Enabling FXP support, however, can make a server vulnerable to a denial-of-service attack, known as the FTP bounce attack
In such a scenario, the "client" is a compromised machine that bombards server B.FXP is also frequently used for warez

Due to these considerations, FXP is often disabled by default on FTP servers.

lftp Man Page

Follow ups


Matt said…
Hello thanks for this great post it is exactly what I'm looking for... I have a folder on my server that I would like to upload to my FTP server daily...

What I would like to happen is that All of the folders inside /Public/WebSites on my server are put into the folder new/Classes on the FTP server.

However what this script does is it puts all the folders of /Public/WebSites into new/Classes/new/Classes on the FTP server. Do you know what I might be able to do to the script to fix it so the at it works as I intend?

Anonymous said…
I guess your local directory /Public/WebSites is having new/Classes inside it. If so can you try resetting LOCALDIR to /Public/WebSites/new/Classes

Popular posts from this blog

Check remote UDP connectivity from Linux

Hi there, You all know how to check TCP port connectivity from a Linux or UNIX machine to a remote machine using telnet as per th example below $ telnet 25 but we can't adopt TELNET to check UDP connectivity. Linux and most of the UNIXes come with a network layer utility called nc (abbreviation for netcat) which is very useful to check UDP connectivity and to explore a lot with both TCP and UDP. An example is shown below # nc -v -u -z -w 3 123 Connection to 123 port [udp/ntp] succeeded!

Squid dead but subsys locked

# /etc/init.d/squid status squid dead but subsys locked # df -h # rm -rf /var/run/ # rm -rf /var/lock/subsys/squid # tail -f /var/log/messages # tail -f /var/log/squid/cache.log # /etc/init.d/squid start 2007/12/28 09:57:16| Starting Squid Cache version 2.5.STABLE6 for i386-redhat-linux-gnu... 2007/12/28 09:57:16| Process ID 26701 2007/12/28 09:57:16| With 1024 file descriptors available 2007/12/28 09:57:16| DNS Socket created at, port 33056, FD 4 2007/12/28 09:57:16| Adding nameserver 202.XX.XX.XXX from /etc/resolv.conf 2007/12/28 09:57:16| Adding nameserver 202.XX.XX.XXX from /etc/resolv.conf 2007/12/28 09:57:16| Adding nameserver from /etc/resolv.conf 2007/12/28 09:57:16| User-Agent logging is disabled. 2007/12/28 09:57:16| Referer logging is disabled. 2007/12/28 09:57:16| Unlinkd pipe opened on FD 9 2007/12/28 09:57:16| Swap maxSize 102400 KB, estimated 7876 objects 2007/12/28 09:57:16| Target number of buckets: 393 2007/12/28 09:57:16| Using 8192

postfix/smtp connect to[]: Connection timed out (port 25) server dropped connection without sending the initial SMTP greeting

Postfix has been configured fine. The mail is working in the local Domain - But when I try to send mails to outer domains,it produces the following "TIMEOUT" errors in /var/log/mail/info Nov 5 23:02:43 mydomain postfix/pickup[30923]: 419941C678: uid=555 from= Nov 5 23:02:43 mydomain postfix/cleanup[31536]: 419941C678: message-id=<> Nov 5 23:02:43 mydomain postfix/qmgr[22926]: 419941C678: from= , size=330, nrcpt=1 (queue active) Nov 5 23:03:13 mydomain postfix/smtp[31538]: connect to[]: Connection timed out (port 25) Nov 5 23:03:13 mydomain postfix/smtp[31538]: connect to[]: server dropped connection without sending the initial SMTP greeting (port 25) Nov 5 23:03:13 mydomain postfix/smtp[31538]: 419941C678: to= , relay=none, delay=30, status=deferred (connect to[]: server dropped connection without sending the initial SMTP greeting) N