check if file exists on remote host with ssh

后端 未结 11 737
醉酒成梦
醉酒成梦 2021-01-31 03:20

I would like to check if a certain file exists on the remote host. I tried this:

$ if [ ssh user@localhost -p 19999 -e /home/user/Dropbox/path/Research_and_Devel         


        
11条回答
  •  面向向阳花
    2021-01-31 03:43

    Test if a file exists:

    HOST="example.com"
    FILE="/path/to/file"
    
    if ssh $HOST "test -e $FILE"; then
        echo "File exists."
    else
        echo "File does not exist."
    fi
    

    And the opposite, test if a file does not exist:

    HOST="example.com"
    FILE="/path/to/file"
    
    if ! ssh $HOST "test -e $FILE"; then
        echo "File does not exist."
    else
        echo "File exists."
    fi
    

提交回复
热议问题