Wednesday, March 22, 2006

FTP Shell script

This post is just to collect comments and feedback on the FTP shell script at:

I notice that the majority of visitors are from Brazil, France, Germany, Italy, Romania, Tawain, and the United Kingdom. So, I am sure the first improvement will be just to translate the page.

I would appreciate hearing about experiences, successes and failures.

Links to this post


At 05 April, 2006 08:39, Anonymous Anonymous said...

Its ok. But if you are invoking this shell script from some other programs(like C,java), How will you handle errors.....

At 10 April, 2006 09:03, Blogger willCode4Beer said...

Actually, if I were using another language, I'd just ftp within the language instead of using the shell script. Besides error handling, as you mentioned, you get better control of the operation, and the "files" you ftp don't ever have to exist as real files. The can be dynamically created data thats just sent down the wire.

For Java, there is some rudimentary built in support for FTP but, the Jakarta Commons Net library is much better. It would be a much better choice than calling native code. Also, calling the shell from Java could invite some security problems.
There are hundreds of libraries providing FTP support in C/C++ as a quick google will show.

At 09 January, 2007 09:34, Anonymous Anonymous said...

Great, thank you. It work for me.

At 10 February, 2008 15:50, OpenID sommi6 said...

Why is it that if you put any bash commands after the second EOF, even a simple 'echo "hello"', you get no response? I was trying to use a lock file, as this script is called by another script and I need to determine if it's still running, since it will have variable lengths of time depending on how many files are being transfered.

At 11 February, 2008 11:39, Blogger willCode4Beer said...

Hi Sommi6,
It's because EOF stands for "End of File"

Probably the simplest way to do what you want is to create a few functions in the bash script that can be called individually.

At 29 April, 2008 06:39, Anonymous Anonymous said...

Great, it works. But Solaris doesn't know the parameter '-u' :-)

At 25 July, 2008 04:10, Anonymous Anonymous said...

This is a nice script ..Is it possible to take IP address from a file .So that I can add new server IP as column and the script read server IP from file and execute the script for each server ? Email me on

At 28 August, 2008 22:48, Anonymous Anonymous said...

A bit of an aside but can you direct an FTP based on the hostname of the puter? ie the same script can be run from multiple puters so I'd like to put something like..

if $hostname="thiscomputer"
ftp -in theothercomputer
if $hostname="theothercomputer"
ftp -in this computer

Does that look like something that should work?

At 01 September, 2008 15:09, Blogger willCode4Beer said...

To both anonymous'

Yes, you should be able to read the ip out of a file.

Even easier, just have the file contain something like:
export ip=
and execute the file from the script. It'll be easier than parsing.

For the hostname, just read it from:
Should work just fine.

At 03 May, 2009 01:50, Anonymous Anonymous said...

is it possible to retrieve the password from the encrypted file system such as /etc/passwd ?

At 04 June, 2009 10:56, Anonymous Anonymous said...

Having a problem setting a path and filename on Rhel 5.0. I get the error "The system could not find the specified path". It works like a champ if the file is in the same directory as the script. Any suggestions?

At 17 August, 2009 01:00, Blogger Dropped Packet said...

Thanks a lot!

At 18 August, 2009 10:21, Blogger rageshis said...

nice one, very helpfull.

At 19 August, 2009 08:07, Blogger saranya said...

Your code is very useful..

At 29 December, 2009 09:15, Blogger Shail said...

Thanks for your efforts.
But unfortunatly didn't worked for me.
and it is asking me password if i execute it.Not sure why this is happening.
please help

At 12 March, 2010 00:27, Blogger laksiri said...

didn't work for me "ftp: u: unknown option " message displayed i used F12

At 01 April, 2010 14:58, Anonymous Anonymous said...

ftp -n would work too . Sometimes you get the error
ftp: u: unknown option

At 23 May, 2010 06:30, Blogger Daniel said...

OK, I have checked, checked and double checked that I have used the right filename/location but I get the following error when using your script:

"/home/danielgroves/text.txt: No such file or directory"

Any Ideas?
I have tried chmodding it to 777.

At 23 June, 2010 16:57, Anonymous Anonymous said...

works for me, thx!

At 08 July, 2010 14:35, Anonymous Anonymous said...

I am trying to do the same from room directly and it's not working. it's throwing below for the put $filename..

Could not create file.

At 17 July, 2010 09:18, Blogger Daniyal said...

Thanks *Beer. You're a lifesaver! I'll read up to figure out how it works! Should help when invoking ne other prompt based apps via bash.

At 21 September, 2010 02:44, Blogger saad said...

i want to take backup of some tables from another pg server using ftp configuration of differennt server in file , can any one help me ?

At 13 December, 2010 22:10, Anonymous Anonymous said...

thanks ...i wanna learn shell scripting... wnt more help

At 17 December, 2010 03:57, Anonymous Anonymous said...


I had to add the -nv option - but wow this really rocks now.

Can you point me to some code examples about how I can then compress the file (Gzip) and move it to the archive location?

At 29 December, 2010 18:43, Blogger rsanchezy said...

Hi I ve tried to create a ftp shell script to move some files from a specific path in ubuntu /home/rick/ABC/files.txt to a host with a different path. When a debug my ftp connection, I see local y remote folder is the same
Server ftp:
user & pass: rick
local path:/ home/rick/NARANJA/files.txt
remote path change according day, month 6 year in a Windows machine

At 29 December, 2010 19:18, Blogger rsanchezy said...

Hi, I have trouble to create a script. I want to transfers some txt files from an ubuntu machine from a specific folder to windows machine, but when I debug a shell with ftp I see local & remote path are the same path

IP Server ftp:
ftp user & pas: rick

onnected to
220-Cerberus FTP Server Personal Edition
220-Welcome to Cerberus FTP Server
220 Created by Grant Averett
ftp: setsockopt: Bad file descriptor
---> SYST
500 Not logged in
---> USER rick
331 User rick, password please
---> PASS rick
230 Password Ok, User logged in
---> TYPE I
200 Type Binary
---> CWD ftproot
250 Change directory ok
local: /home/rick/NARANJA/hoy.tar.gz remote: /home/rick/NARANJA/hoy.tar.gz
---> PORT 192,168,1,20,135,107
200 Port command received
---> STOR /home/rick/NARANJA/hoy.tar.gz
500 Invalid Path
---> QUIT
221 Goodbye
My script:
cd /home/rick/NARANJA
ftp -ivnd $hostname <<EOF
quote USER $username
quote PASS $password

cd ftproot
put $filename

At 29 June, 2011 06:10, Anonymous Anonymous said...

how to select local directory


Post a Comment

Subscribe to Post Comments [Atom]

<< Home

Links to this post on:

Create link here by posting on Blogger