LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-21-2004, 08:09 AM   #1
guarriman
Member
 
Registered: Nov 2004
Posts: 101

Rep: Reputation: 15
How to write a shell script to download a file via FTP?


Hi.

I would like to automate a process to download a file from my FTP server.

Generally, I perform it by using:
$ ftp foo.com
Name> joe
Password> xxxx
ftp> cd /home/joe
ftp> get myfile.tar.gz

But I have no clue idea about how to write it with a shell script. Any idea?
 
Old 12-21-2004, 08:37 AM   #2
AxeZ
Member
 
Registered: Sep 2002
Location: Novi Sad, Vojvodina
Distribution: Slackware, FreeBSD
Posts: 386

Rep: Reputation: 32
#!/bin/sh
wget -c joe:xxxxx@ftp.site.com/home/joe/myfile.tar.gz
 
Old 12-21-2004, 09:46 AM   #3
guarriman
Member
 
Registered: Nov 2004
Posts: 101

Original Poster
Rep: Reputation: 15
wget -c joe:xxxxx@ftp.site.com/home/joe/myfile.tar.gz: Unsupported scheme.

When I login via FTP at ftp.site.com, I launch in /home/joe, and 'myfile.tar,gz' is
at '/home/joe/myfile.tar.g'.


SOLVED
wget -c ftp://joe:xxxxx@ftp.site.com/myfile.tar.gz

INCLUDE: ftp://
REMOVE: /home/joe

Last edited by guarriman; 12-21-2004 at 10:02 AM.
 
Old 12-21-2004, 09:58 AM   #4
anon237
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

You can use a here-document. Here's a quick example:

Code:
#!/bin/sh

HOST='some.site.com'
USER='yourid'
PASSWD='yourpw'
FILE='file.txt'

ftp $HOST <<END_SCRIPT
user $USER
$PASSWD
put $FILE
quit
END_SCRIPT
exit 0
Hope this helps.
 
Old 12-21-2004, 10:31 AM   #5
pcunix
Member
 
Registered: Dec 2004
Location: MA
Distribution: Various
Posts: 149

Rep: Reputation: 23
There are lots of ways to do this.

A here document as suggested above is useful, as is wget etc.

Slightly better (but only slightly) is to use a .netrc file

here's a basic .netrc (see man netrc) for normal ftp.

----$HOME/.netrc 600 perms --
machine somewhere.com login mylogin password mypass macdef
init
lcd /appl/fp/merge
cd /appx/data/50/XFR/Data
put artrx.tab TRXFER.dat
quit

machine someothermachine.org login whatever password pass macdef
init
hash
bin
prompt off

machine yetanother ...


The first example (somewhere.com) logs in, changes to a local directory /appl/fp/merge, then changes to /appx/data/50/XFR/Data on the server and "puts" a file.

With this in place, the command "ftp somewhere.com" will do the "put". You could set "prompt off" and use "mput" or "mget" in the .netrc also.

The second just logs you in to "someothermachine.org" , turns on hash, etc. and then you can type your own commands.

You can fully script more complex things with:

#!/bin/ksh
echo "machine somewhere.com login mylogin password mypass
macdef init" > $HOME/.netrc
echo "lcd /appl/fp/merge" >> $HOME/.netrc
echo "cd /appx/data/50/XFR/Data" >> $HOME/.netrc
for i in *.tab
do
echo "put $i ${i%tab}.dat" >> $HOME/.netrc
done
echo "quit" >> $HOME/.netrc
echo " " >> $HOME/.netrc
# always end a macdef with a blank line
chmod 600 $HOME/.netrc
ftp somewhere.com

Kermit has a better ftp scripting capability

http://www.columbia.edu/kermit/ftpclient.html
http://www.columbia.edu/kermit/ftpscripts.html

and so does ncftp and I think lftp


Of all the ways, I prefer Perl - see http://aplawrence.com/Unixart/perlnetftp.html for the why and the how.


--
Tony Lawrence
http://aplawrence.com
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
HELP:write a CGI program with shell script supermyself Programming 3 06-12-2005 04:10 PM
How can I write a shell script to transfer files and keep permissions? LancerNZ Linux - Networking 1 02-24-2005 04:49 AM
write a shell script to send email yenonn Programming 4 02-11-2005 11:06 PM
Help needed learning to write a shell script for logons nistur Linux - Networking 8 10-02-2004 03:34 AM
[SHELL SCRIPT] Write at the right of the shell window Creak Linux - General 2 04-02-2004 04:00 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration