# check for file existence on remote machine using sftp

**URL:** <https://community.unix.com/t/check-for-file-existence-on-remote-machine-using-sftp/260805>\
**Category:** Shell Programming and Scripting\
**Created:** [March 2, 2010, 2:12am UTC](https://community.unix.com/t/check-for-file-existence-on-remote-machine-using-sftp/260805 "2010-03-02T02:12:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![narasimha123](https://community.unix.com/letter_avatar/narasimha123/32/6_22734026db12803ebb3e059622ac3534.png) [@narasimha123](https://community.unix.com/u/narasimha123)\
**Post date:** [March 2, 2010, 2:12am UTC](https://community.unix.com/t/check-for-file-existence-on-remote-machine-using-sftp/260805/1 "2010-03-02T02:12:59Z")

</div>

Hi all,  
I am a beginner to shell script.Can any one please help me on the below requirement

I need to check whether the file (called 3Com\_Files\_Delivered.txt) exists on the remote mechaine or not? if so i need to copy all the files from there to my local mechaine.Especially i am struggling to get how to check whethere a particular file exists or not on remote mechaine.Please help me on this.

Advanced thanks  
Narasimharao.

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [March 2, 2010, 2:26am UTC](https://community.unix.com/t/check-for-file-existence-on-remote-machine-using-sftp/260805/2 "2010-03-02T02:26:58Z")

</div>

You could try to ftp 3Com\_Files\_Delivered.txt to your local host. From the subsequent presence of a copy of that file on your system you can conclude that transfer was successful. If not, then repeat after 5 minutes. If it was successful, then that is the green light to immediately start the transfer of the actual data file and delete the transferred copy of the indicator file. Perhaps there are more sophisticated methods, bit this is simple and the commands for both ftp transfers are almost identical.

---

<div class="post-metadata">

**Author:** ![devtakh](https://community.unix.com/letter_avatar/devtakh/32/6_22734026db12803ebb3e059622ac3534.png) [@devtakh](https://community.unix.com/u/devtakh)\
**Post date:** [March 2, 2010, 2:28am UTC](https://community.unix.com/t/check-for-file-existence-on-remote-machine-using-sftp/260805/3 "2010-03-02T02:28:36Z")

</div>

Please read manual of rsh. It should be able to help youexecuting a script on the remote machine.

```nohighlight
if [-e filename]; then
echo "file exists"
else
echo "file not exists"
fi

```

cheers,  
Devaraj Takhellambam

---

<div class="post-metadata">

**Author:** ![Nila](https://community.unix.com/user_avatar/community.unix.com/nila/32/1187_2.png) [@Nila](https://community.unix.com/u/Nila)\
**Post date:** [March 2, 2010, 2:35am UTC](https://community.unix.com/t/check-for-file-existence-on-remote-machine-using-sftp/260805/4 "2010-03-02T02:35:44Z")

</div>

Try this,

```nohighlight
#!/bin/sh

hostname=abc@server

if [-n `ssh $hostname 'ls /home/user/file 2>/dev/null'`]; then
        echo file exists
else
        echo "file doesn't exist"
fi

```

---

<div class="post-metadata">

**Author:** ![narasimha123](https://community.unix.com/letter_avatar/narasimha123/32/6_22734026db12803ebb3e059622ac3534.png) [@narasimha123](https://community.unix.com/u/narasimha123)\
**Post date:** [March 2, 2010, 4:12am UTC](https://community.unix.com/t/check-for-file-existence-on-remote-machine-using-sftp/260805/5 "2010-03-02T04:12:14Z")

</div>

i tried all the above mentioned options.but i am not able to chk the file existence.

here i should use only sftp since my client is using sftp.

please see my code and guide me how to add the file check condition to that existing code

```nohighlight
. /opt/etl/bin/.etl_profile.ksh
# echo "the variable is:" $3COM_SFTP_ACTN

Tcomdta=/var/opt/etl/datsrc/3com/data
Tcomtmp=/var/opt/etl/datsrc/3com/temp
Tcomlog=/var/opt/etl/datsrc/3com/log
logdate=`date +%Y%m%d%H%M%S`;
logfilenm=$Tcomlog/"3com_FTP_$logdate.log"
FILE="3Com_*"
maillist=`eval echo $TCOM_MAIL_LIST`

#----Log Errors in Log File---------#

err() {
 echo "ERROR: $@" >> $logfilenm
        exit 1
}

#-----Move input file(s) from SFTP Server to 3com folder-------#

cd $Tcomtmp

echo Log BEGIN For $logdate>$logfilenm
>$Tcomtmp/file.dat

sftp -b - $TCOM_SFTP_ACTN << EOF >$logfilenm
mget /$FILE
quit
EOF

```

up to this part is working fine.But i was not able to add the file chek part to this.

Any one please help me on this.This is urgent.

appreciate all your help

thanksinadvance  
narasimha

---------- Post updated at 04:12 AM ---------- Previous update was at 03:35 AM ----------

plz any one reply me on the above.

thanks  
narasimha

---

<div class="post-metadata">

**Author:** ![nj78](https://community.unix.com/user_avatar/community.unix.com/nj78/32/1704_2.png) [@nj78](https://community.unix.com/u/nj78)\
**Post date:** [March 2, 2010, 1:03pm UTC](https://community.unix.com/t/check-for-file-existence-on-remote-machine-using-sftp/260805/6 "2010-03-02T13:03:31Z")

</div>

That ssh check above should work fine, sftp and ssh use same tunnell, see man page:

sftp is an interactive file transfer program, similar to ftp(1), which performs all operations over an encrypted ssh(1) transport.

---

<div class="post-metadata">

**Author:** ![narasimha123](https://community.unix.com/letter_avatar/narasimha123/32/6_22734026db12803ebb3e059622ac3534.png) [@narasimha123](https://community.unix.com/u/narasimha123)\
**Post date:** [March 17, 2010, 3:16am UTC](https://community.unix.com/t/check-for-file-existence-on-remote-machine-using-sftp/260805/7 "2010-03-17T03:16:42Z")

</div>

hi All,  
Thanks for all your thoughts on this.Finally i am able to code a program which ftp's the files from remote machine to my local machine.But a part from this i need to add a process here i am giving full details .please guide me on this.

i need to add a process to the below existing script which has to poll every 5 minutes until 12 P.M from 5 A.M .How can i add this to the below script.Please advice me.

every my script will be triggerd at 5 A.M through scheduler.

```nohighlight
comdta=/var/opt/etl/datsrc/3com/data
cominp=/var/opt/etl/datsrc/3com/input
comtmp=/var/opt/etl/datsrc/3com/temp
comlog=/var/opt/etl/datsrc/3com/log
logdate=`date +%Y%m%d%H%M%S`;
lognm="3com_FTP_$logdate.log"
trgr_file="3Com_Files_Delivered.txt"
infile="3Com_*"
maillist=`eval echo $TCOM_MAIL_LIST`
flag="N"

####################################################################
#
#REMOVING LOG FILES OLDER THAN 30 DAYS
#
####################################################################

find $comlog -type f -name '3com_FTP_*.log' -follow -mtime +30 -exec rm {} \; -print

#----Log Errors in Log File---------#

inf_log()
{
	echo "INFO: $@" >> $comlog/$lognm
}

err()
{
	echo "ERROR: $@" >> $comlog/$lognm
	exit 1
}

#######################################################################
#
# FTP PROCESS To check the existence of the trigger file every 5 min
# and if exist move all the input files from SFTP Server to 3com folder
#
#######################################################################

cd $cominp

echo Log BEGIN For $logdate>$comlog/$lognm

while ["$flag" != "Y"]
do
sftp -b - $TCOM_SFTP_FILES <<EOF >$comlog/$lognm
get /$trgr_file
bye
EOF

if [[-f $trgr_file]]; then
inf_log "Trigger File Exist on SFTP Server........."
inf_log "Copying 3Com Files Started.........."
sftp -b - $TCOM_SFTP_FILES <<EOF >$comlog/$lognm
mget /$infile
bye
EOF
flag='Y'
else
sleep 300
fi
done

#--------Now copying Plan,Pipeline,Forecast file(s) from input to data directory-------#

cp 3Com_Plan* $comdta
cp 3Com_Pipeline* $comdta
cp 3Com_Forecast* $comdta

#-------Mail SFTP status----------------------------#

grep -q 'Fetching' $comlog/$lognm

if [$? -eq 0]; then
	inf_log "SFTP File Transfer Success!!!"
else
	echo "SFTP File Trasnsfer Failed\n"|mailx -s "3COM File Transfer Failed -ERROR" $maillist
	err " SFTP file transfer Failed - File is not Found!!!"
	exit 1
fi

#-------Remove input file(s) from SFTP Server-------#

sftp -b - $TCOM_SFTP_FILES <<EOF >>$comlog/$lognm
cd /
rm $infile
quit
EOF

#-------Notifying SFTP File(s) Removal Status-------#

grep -q 'not found' $comlog/$lognm
if [$? -eq 0]; then
	err " Removal of 3Com File(s) from SFTP Box Failed!!!"
else
	inf_log " Removal of 3Com File(s) from SFTP Box is Success!!!"
	exit 0
fi

```

THANKS  
narasimharao

---

<div class="post-metadata">

**Author:** ![PradeepKr](https://community.unix.com/letter_avatar/pradeepkr/32/6_22734026db12803ebb3e059622ac3534.png) [@PradeepKr](https://community.unix.com/u/PradeepKr)\
**Post date:** [August 30, 2010, 6:50am UTC](https://community.unix.com/t/check-for-file-existence-on-remote-machine-using-sftp/260805/8 "2010-08-30T06:50:49Z")

</div>

Can we give URL in place of absolute path of the remote file?
