check if file exists on local or remote host with ssh

less than 1 minute read

check local file

FILE=/mnt/glusterfs/pg-php/deploy/staging/api.tar

if [ -f "$FILE" ] ;
then
    echo "staging $FILE exists."
    exit 1
else
	echo "staging File does not exist"
fi

check if file exists on remote host with ssh

https://stackoverflow.com/questions/12845206/check-if-file-exists-on-remote-host-with-ssh

#!/bin/bash
USE_IP='-o StrictHostKeyChecking=no username@192.168.1.2'

FILE_NAME=/home/user/file.txt

SSH_PASS='sshpass -p password-for-remote-machine'

if $SSH_PASS ssh $USE_IP stat $FILE_NAME \> /dev/null 2\>\&1
    then
        echo "File exists"
    else
        echo "File does not exist"
fi

Updated: