Shell script to get all the files from FTP server

 
Thread Tools Search this Thread
Homework and Emergencies Emergency UNIX and Linux Support Shell script to get all the files from FTP server
# 1  
Old 09-30-2010
Shell script to get all the files from FTP server

Hi Guru's,

I am new to UNIX. my requirement is to log on to FTP server and get all the .txt files. i have developed one script by searching some forums but getting error and not able to fix them. pls see below code.
Code:
         
         ftp -i-n<<EOF
                open $FTP_HOST
                user $FTP_USER $FTP_PASS
                cd FTP_PATH
                ascii
                  for file in `ls *.txt 2>> $LOGFILE`
                    do
                      if [ ! -f $file ]; then
                        echo " `date` Files were not received " 
                      exit
                      else
                       lcd $LCK_IN_DIR
                       get $file
                      fi
                     done
                   bye
                   EOF

i am getting the below error when i run the script.
Code:
./ftp.sh[56]: syntax error at line 81 : `<<' unmatched

some how i have fixed that error. But script did not logon to FTP server automatically. when i run the script it is prompting to userid/password.

Appreciate your help

Thanks
Arun

Moderator's Comments:
Mod Comment Having so many posts you should be familiar using code tags.
# 2  
Old 09-30-2010
the EOF must be at the very beginning of the line (no spaces)

But regardless, your approach is sadly not the right one. Use "mget *" to get all the files. Your for-loop in the FTP command just wont work.

A standard alternative is to use perl with the Net::FTP module (search these forums for that). Another approach is with "Expect". Again, search these forums.
# 3  
Old 09-30-2010
Code:
#!/usr/bin/env ruby 
require 'net/ftp'
user="anonymous"
pass="password"
server="localhost"
Net::FTP.open(server) do |ftp|
    ftp.login(user,pass)
    ftp.nlst("*.txt").each do |file|
       ftp.getbinaryfile(file,file)
       ftp.close
    end
end

This User Gave Thanks to kurumi For This Post:
# 4  
Old 09-30-2010
Sexy. I like that. How do install ruby? Smilie
# 5  
Old 09-30-2010
Thanks KURUMI,

I forgot to mention couple of points here.
I am using HP UNIX and i need download all the files in ASCII mode. will the above script work?
Thx
Arun
# 6  
Old 09-30-2010
hi otheus, please see Download Ruby
Depending on your distribution, there might be a package manager that makes installation easy.

---------- Post updated at 08:52 AM ---------- Previous update was at 08:47 AM ----------

Quote:
Originally Posted by arund_01
I am using HP UNIX
sorry, HP UNIX does have Ruby by default. So unless you install it, you can try Perl instead. But judging from what you already have, you can simply use
Code:
mget *.txt

# 7  
Old 09-30-2010
Quote:
Originally Posted by kurumi
hi otheus, please see Download Ruby
Depending on your distribution, there might be a package manager that makes installation easy.

---------- Post updated at 08:52 AM ---------- Previous update was at 08:47 AM ----------


sorry, HP UNIX does have Ruby by default. So unless you install it, you can try Perl instead. But judging from what you already have, you can simply use
Code:
mget *.txt

Hi Kurumi,

I think ruby is not installed and I am getting error. Could you pls provide sample code using Perl. I have tried manually mget *.txt in FTP server. looks like mget is not working in FTP. Will it work with perl?

Thanks for your help again.

Thanks
Arun
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Download latest file via ftp server unix through shell script

Hello this is my first post in this forum , I dont want to be unhappy.. I am writing one script but facing difficulty to find the latest file with some new pattern My requirement is 1. The file is coming like "ABCD-23220140303" at FTP server once in a week. 2. script will run on daily... (3 Replies)
Discussion started by: ajju
3 Replies

2. Shell Programming and Scripting

Need help creating a script to FTP files to a server and then delete the files that were transfered.

I am trying to FTP files to a Windows server through my Linux machine. I have setup the file transfer with no problems but am having problem deleting those files from the Linux box. My current non-working solution is below. Any ideas, anyone?? :wall: Please be gentle, I'm fairly new to this... (4 Replies)
Discussion started by: jmalfhs
4 Replies

3. Shell Programming and Scripting

Automated script to look for files in FTP Server location.

suppose one file comes in one sever location on MOnday.we have to write a script to automatically get that files and put it in different server location. ---------- Post updated at 10:28 AM ---------- Previous update was at 10:27 AM ---------- Please help me on this (2 Replies)
Discussion started by: sonam273
2 Replies

4. Shell Programming and Scripting

script for to take files from FTP server to UNIX server and Unzipped that files

script for to take files from FTP server to UNIX server and Unzipped that files (1 Reply)
Discussion started by: sunilamarnadh
1 Replies

5. UNIX for Dummies Questions & Answers

ftp files from one server to another using shell script

Hi Guys Any Help I have created a spool file that i need to copy onto another server using FTP in a shell script both servers are linux (3 Replies)
Discussion started by: itai
3 Replies

6. Shell Programming and Scripting

shell script to ftp the files from windows to unix server

Hi, I need to ftp some input files from windows to unix server.All the files will be saved in the C drive in my machine. Currently all these files are transferring manually to the unix server.I need to write a shell script which ftp the files from windows to unix box.When I searched in the... (10 Replies)
Discussion started by: kavithakuttyk
10 Replies

7. Shell Programming and Scripting

Shell script for FTP a file from one server to another server

Hi Can any one help me for script. I need a script and this script has to execute daily to move the files. FTP from one server to another server EX: Sep 10 06:24 abc.txt Sep 11 06.56 abc.txt Sep 12 08.23 abc.txt these files are located at a/b/c/e/f in 123 server and copy... (0 Replies)
Discussion started by: mahantysk
0 Replies

8. Shell Programming and Scripting

FTP from Linux to windows server by shell script

Hi, Please advice whether my below requirement is feasible, My requirement : Automated FTP from linux server to windows server using a shell script on every monday. If feasible, please help me how to do this ? Thanks in advance (2 Replies)
Discussion started by: apsprabhu
2 Replies

9. Shell Programming and Scripting

Shell Script to ftp from windows server

Hello All, I've to write a shell script to transfer some files to/from windows server. I can put & get files simply by doing ftp but need to automate this. So I tried for this -. HOST=.hostname USER='username' PASSWD='***' ftp -n $HOST <<END_SCRIPT quote USER $USER quote PASS... (8 Replies)
Discussion started by: shilpa.rajput
8 Replies

10. Shell Programming and Scripting

how to move a file from one server to another server using FTP in shell script?

I have a file --> file1.txt i need to copy this file to another server using FTP....the 2 servers are server1 and server2..may i know how to write a script that can do this? thanks in advance! Im a newbie to this... (4 Replies)
Discussion started by: forevercalz
4 Replies
Login or Register to Ask a Question