how to check if file exists on ftp server using shell script

6,074 views
Skip to first unread message

Jetal Desai

unread,
Sep 5, 2008, 6:08:01 PM9/5/08
to vg...@googlegroups.com
Hi,
 
I want to download some files from windows server using ftp but before
downloading that how can i check that file exists or not on windows server.
I will run this script using cron so it should be in non intrective mode.

--
Thanks & Regards
Jetal Desai
Ventech Solutions
Email: jetal...@gmail.com
Office: 847-964-8661
Mobile: 614-302-0786

Sham Arsiwala

unread,
Sep 6, 2008, 12:14:04 AM9/6/08
to VG...@googlegroups.com
i think using ftp with nlist command you can get the list of files on ftp server, then using some script you can check whether you have that file or not. if not then perform mget for the files which are not there
--
Regards
SHAM ARSIWALA.
Senior System & Network Administrator
Electromech
Baroda.

Bhatt Niraj

unread,
Sep 6, 2008, 1:25:14 AM9/6/08
to VGLUG
Hi,

As per my knowledge, you can not pass user name and password together
in simple ftp command.
I prefer ncftp, i used in my shell script, you can pass user name and
password together and finished all ftp related task.

Thanks
Niraj Bhatt

On Sep 6, 9:14 am, "Sham Arsiwala" <shamarsiw...@gmail.com> wrote:
> i think using ftp with nlist command you can get the list of files on ftp
> server, then using some script you can check whether you have that file or
> not. if not then perform mget for the files which are not there
>
>
>
> On Sat, Sep 6, 2008 at 3:38 AM, Jetal Desai <jetalde...@gmail.com> wrote:
> > Hi,
>
> > I want to download some files from windows server using ftp but before
> > downloading that how can i check that file exists or not on windows server.
> > I will run this script using cron so it should be in non intrective mode.
>
> > --
> > Thanks & Regards
> > Jetal Desai
> > Ventech Solutions
> > Email: jetalde...@gmail.com

Jetal Desai

unread,
Sep 8, 2008, 11:57:23 AM9/8/08
to VG...@googlegroups.com
Hi,
 
you can pass username and password in ftp together in script for ftp.

Office: 847-964-8661
Mobile: 614-302-0786

kaushal patel

unread,
Sep 8, 2008, 3:28:15 PM9/8/08
to VG...@googlegroups.com
Hi

Please find below given script which is working on one of my server
# !/bin/sh

ftp -n  1.1.1.1 <<EOF
user $USER $PASSWD
lcd /opt/remote
binary
prompt
ls -l > list.txt
mget list.txt
quit
EOF
exit
# END OF SCRIPT

now put of conditions by reading list.txt on your location m/c. And download if you find required files.

More options explaination are as below.
1. What is the "-n" paramter do for the ftp command

man ftp
     -n        Do not attempt "auto-login" upon  initial  connec-
               tion.  If  auto-login is not disabled, ftp  checks
               the .netrc file in the user's home  directory  for
               an  entry  describing  an  account  on  the remote
               machine.  If no entry exists, ftp  will prompt for
               the  login  name  of  the  account  on  the remote
               machine (the default is  the  login  name  on  the
               local  machine),  and, if necessary, prompts for a
               password and an account with which to login.

2. Why do you need "<<EOF" at the end of the ftp command.
      To tell the FTP program when to "exit"

3. What does the "binary" command do?
     set ftp transfer in "binary" mode



Thanks
Kaushal
--
Kaushal Patel

Jetal Desai

unread,
Sep 8, 2008, 4:11:12 PM9/8/08
to VG...@googlegroups.com
thanks for the reply..
 
But requirement is little diffrent.
 
I want to check if files exists on the ftp location and if exists then get those files.

i dont want to check the entire list of files on the ftp server.
 

Office: 847-964-8661
Mobile: 614-302-0786

kaushal patel

unread,
Sep 8, 2008, 4:14:51 PM9/8/08
to VG...@googlegroups.com
Well I think now its pretty close....Little more logic.....

For eg... ls -l kaushal.txt

If you know regex and your files are having common patter..

             ls -l ^k*.txt        ( Any file that starts with k)..

I believe you know the files you want... So now easy....

Kaushal
--
Kaushal Patel

ElectroMech

unread,
Sep 8, 2008, 10:59:56 PM9/8/08
to VG...@googlegroups.com
That is good.

But try lftp it can do lots of thing non interactive way.

For more info

man lftp.
--

--
Nilesh Vaghela
ElectroMech
Redhat Channel Partner and Training Partner
16, Sun Rise complex,
Nr. Mansi cross Road,
Satellite Rd, Ahmedabad
25, The Emperor, Fatehgunj, Baroda.
www.electromech.info

ElectroMech

unread,
Sep 8, 2008, 11:09:16 PM9/8/08
to VG...@googlegroups.com

Jetal Desai

unread,
Sep 9, 2008, 10:14:34 AM9/9/08
to VG...@googlegroups.com
thanks.

Office: 847-964-8661
Mobile: 614-302-0786
Reply all
Reply to author
Forward
0 new messages