check if file exists on remote host with ssh

后端 未结 11 735
醉酒成梦
醉酒成梦 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:24

    In addition to the answers above, there's the shorthand way to do it:

    ssh -q $HOST [[ -f $FILE_PATH ]] && echo "File exists" || echo "File does not exist";
    

    -q is quiet mode, it will suppress warnings and messages.

    As @Mat mentioned, one advantage of testing like this is that you can easily swap out the -f for any test operator you like: -nt, -d, -s etc...

    Test Operators: http://tldp.org/LDP/abs/html/fto.html

提交回复
热议问题