Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to find a file exists in particular dir through SSH

Tags:

bash

ssh

how to find a file exists in particular dir through SSH

for example : host1 and dir /home/tree/TEST

Host2:- ssh host1 - find the TEST file exists or not using bash

like image 449
Tree Avatar asked Mar 16 '11 12:03

Tree


2 Answers

ssh will return the exit code of the command you ask it to execute:

if ssh host1 stat /home/tree/TEST \> /dev/null 2\>\&1
then 
  echo File exists
else 
  echo Not found
fi

You'll need to have key authentication setup of course, so you avoid the password prompt.

like image 97
Erik Avatar answered Sep 27 '22 02:09

Erik


This is what I ended up doing after reading and trying out the stuff here:

FileExists=`ssh host "test -e /home/tree/TEST && echo 1 || echo 0"`

if [ ${FileExists} = 0 ]
  #do something because the file doesn't exist
fi

More info about test: http://linux.die.net/man/1/test

like image 44
mikeytown2 Avatar answered Sep 25 '22 02:09

mikeytown2