LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-03-2009, 02:18 AM   #1
vishal_titre
LQ Newbie
 
Registered: Jul 2006
Location: Pune
Distribution: Redhat
Posts: 22

Rep: Reputation: 15
Smile Need Ftp Shell script


Hi all,

I have one requirement .
I want to write shell script which will connect to ftp server , and download the file from ftp server dir to my local server directory , but before downloading the file from ftp server it should check weather the file is already present in local server dir . If file is already present in local server directory then don't download the file , if file is not present in local server directory then download the file to local server dir.

Thank you
vishal Titre
 
Old 02-03-2009, 02:25 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,498

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
What have you tried so far? Have you some specific problem? Checking the existence of a file is as simple as
Code:
#!/bin/bash
file=name_of_the_file
if [ -f $file ]
then
  echo file $file already exists
else
  echo downloading file $file
  ftp blah blah blah
fi
 
Old 02-03-2009, 02:36 AM   #3
vishal_titre
LQ Newbie
 
Registered: Jul 2006
Location: Pune
Distribution: Redhat
Posts: 22

Original Poster
Rep: Reputation: 15
need help on ftp script

Thank you for reply ,
But what i want is before downloading the file from ftp server i want to check weather the file present in ftp server is already present in local server , if yes then i don't want to downlaod , if not present then i want to download file from ftp server to local server ..
Hope i am able to explan my query properly .


Thank you
Vishal titre
 
Old 02-03-2009, 03:03 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,498

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
So, you want to retrieve the list of files on the remote server, then check if they are already present on the local machine? You have two alternatives:

1) if you can guess the name of the file you can try wget with -nc option: this will not retrieve the file if it is already present. For example suppose the file "testfile" is already on the local machine:
Code:
$ ls testfile
testfile
$ wget -nc --ftp-user=USER --ftp-password=PASSWORD ftp://remote.host.address/testfile
File 'testfile' already there; not retrieving.
2) if you don't know the name of the file to download, you can connect two times: the first to do a list of the remote files, the second to actually download the file if it is not already on the local machine, for example:
Code:
#!/bin/bash
# ------------------------------------------------------------------
#  Retrieve the list of files on the remote machine
# ------------------------------------------------------------------
/usr/bin/ftp -ni remote.host.address <<-EOS 1> test.log 2>> test.err
	user USER PASSWORD
	ls
	bye
	EOS
# -------------------------------------------------------------------
#  Extract the file names from the list
# -------------------------------------------------------------------
filelist=$(awk '/^-/{print $NF}' test.log)
# -------------------------------------------------------------------
#  Loop over the list and check the existence of each file:
#  if not existent download it
# -------------------------------------------------------------------
for file in $(echo $filelist)
do
  if [ -f $file ]
  then
    echo file $file already exists: skipping
  else
    /usr/bin/ftp -ni remote.host.address <<-EOS
	user USER PASSWORD
        binary
	get $file
	bye
	EOS
  fi
done
Hope this helps!

Last edited by colucix; 02-03-2009 at 03:05 AM.
 
  


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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
automated FTP shell script RedOctober45 Linux - Software 1 01-09-2008 08:58 AM
Auto ftp shell script mcrosby Linux - Newbie 9 06-27-2007 06:09 AM
FTP in a Bash Shell Script redirection sleepyhomme Programming 2 05-02-2007 11:53 AM
ftp through shell script HSN Programming 24 02-23-2007 03:18 PM
shell script for ftp javier_ccs Programming 7 07-18-2006 11:02 PM


All times are GMT -5. The time now is 02:32 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration