# SFTP check if file exists

**URL:** <https://community.unix.com/t/sftp-check-if-file-exists/297320>\
**Category:** Shell Programming and Scripting\
**Created:** [October 17, 2011, 3:30am UTC](https://community.unix.com/t/sftp-check-if-file-exists/297320 "2011-10-17T03:30:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![phpsnook](https://community.unix.com/letter_avatar/phpsnook/32/6_449c533f586939b6e5f246632a46421f.png) [@phpsnook](https://community.unix.com/u/phpsnook)\
**Post date:** [October 17, 2011, 3:30am UTC](https://community.unix.com/t/sftp-check-if-file-exists/297320/1 "2011-10-17T03:30:31Z")

</div>

I lookifn for a way to SFTP a file based on the presence of a flag file .

The logic is some thing like this in shelling terms

IF [[! -s file.DDMM.done]]; then  
download file.DDMM.dat  
FI

The above is just the way of interpreting the logic.

Just want to add that i do not have SSh access to the remote server

---

<div class="post-metadata">

**Author:** ![jayan\_jay](https://community.unix.com/user_avatar/community.unix.com/jayan_jay/32/1006_2.png) [@jayan\_jay](https://community.unix.com/u/jayan_jay)\
**Post date:** [October 17, 2011, 9:13am UTC](https://community.unix.com/t/sftp-check-if-file-exists/297320/2 "2011-10-17T09:13:58Z")

</div>

try this ..

```nohighlight
if [! -s "file.DDMM.done"]
then
        ftp -vin >> ftp.log <<!
        open remote_IP
        user $USER $PWD
        cd /path/in/remote/server
        get file.DDMM.dat
        bye
        !
else
        echo "File exists"
fi

```

---

<div class="post-metadata">

**Author:** ![phpsnook](https://community.unix.com/letter_avatar/phpsnook/32/6_449c533f586939b6e5f246632a46421f.png) [@phpsnook](https://community.unix.com/u/phpsnook)\
**Post date:** [October 22, 2011, 12:58pm UTC](https://community.unix.com/t/sftp-check-if-file-exists/297320/3 "2011-10-22T12:58:26Z")

</div>

i figured out a way to use batch file to check if the done file is there. i find is the file is there using the verbose mode of sftp. downside of using batch is that there will not be any return codes.

Well fo rnow it works . is there a better way ? would love to do it that way ..
