check if file exists on remote host with ssh

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

    Here is a simple approach:

    #!/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
    

    You need to install sshpass on your machine to work it.

    0 讨论(0)
  • 2021-01-31 03:21
    ssh -q $HOST [[ -f $FILE_PATH ]] && echo "File exists"
    

    The above will run the echo command on the machine you're running the ssh command from. To get the remote server to run the command:

    ssh -q $HOST "[[ ! -f $FILE_PATH ]] && touch $FILE_PATH"
    
    0 讨论(0)
  • 2021-01-31 03:23

    I wanted also to check if a remote file exist but with RSH. I have tried the previous solutions but they didn't work with RSH.

    Finally, I did I short function which works fine:

    function existRemoteFile ()
    {
    REMOTE=$1
    FILE=$2
    RESULT=$(rsh -l user $REMOTE  "test -e $FILE && echo \"0\" || echo \"1\"")
    if [ $RESULT -eq 0 ]
    then
        return 0
    else
        return 1
    fi
    }
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-01-31 03:24

    one line, proper quoting

    ssh remote_host test -f "/path/to/file" && echo found || echo not found
    
    0 讨论(0)
  • 2021-01-31 03:28

    You can specify the shell to be used by the remote host locally.

    echo 'echo "Bash version: ${BASH_VERSION}"' | ssh -q localhost bash
    

    And be careful to (single-)quote the variables you wish to be expanded by the remote host; otherwise variable expansion will be done by your local shell!

    # example for local / remote variable expansion
    {
    echo "[[ $- == *i* ]] && echo 'Interactive' || echo 'Not interactive'" | 
        ssh -q localhost bash
    echo '[[ $- == *i* ]] && echo "Interactive" || echo "Not interactive"' | 
        ssh -q localhost bash
    }
    

    So, to check if a certain file exists on the remote host you can do the following:

    host='localhost'  # localhost as test case
    file='~/.bash_history'
    if `echo 'test -f '"${file}"' && exit 0 || exit 1' | ssh -q "${host}" sh`; then
    #if `echo '[[ -f '"${file}"' ]] && exit 0 || exit 1' | ssh -q "${host}" bash`; then
       echo exists
    else
       echo does not exist
    fi
    
    0 讨论(0)
提交回复
热议问题