使用ssh检查远程主机上是否存在文件

时间:2012-10-11 17:29:15

标签: linux bash shell ssh

我想检查远程主机上是否存在某个文件。 我试过这个:

$ if [ ssh user@localhost -p 19999 -e /home/user/Dropbox/path/Research_and_Development/Puffer_and_Traps/Repeaters_Network/UBC_LOGS/log1349544129.tar.bz2 ] then echo "okidoke"; else "not okay!" fi
-sh: syntax error: unexpected "else" (expecting "then") 

12 个答案:

答案 0 :(得分:40)

除上述答案外,还有简便的方法:

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

-q是安静模式,它会禁止警告和消息。

正如@Mat所提到的,这样的测试的一个优点是,您可以轻松地为您喜欢的任何测试操作符换出-f-nt-d,{{1}等等......

测试操作员: http://tldp.org/LDP/abs/html/fto.html

答案 1 :(得分:34)

这是一个简单的方法:

if ssh $HOST stat $FILE_PATH \> /dev/null 2\>\&1
            then
                    echo "File exists"
            else
                    echo "File does not exist"

fi

答案 2 :(得分:21)

不能比这简单得多:)

ssh host "test -e /path/to/file"
if [ $? -eq 0 ]; then
    # your file exists
fi

答案 3 :(得分:8)

一行,正确引用

ABORTED

答案 4 :(得分:6)

你错过了;。如果将所有内容放在一行中,则通用语法为:

if thing ; then ... ; else ... ; fi

thing几乎可以返回退出代码。如果then返回0,则采用thing分支,否则采用else分支。

[不是语法,而是test程序(查看ls /bin/[,它实际存在,man test用于文档 - 虽然也可以有内置的在具有不同/附加功能的版本中。)用于测试文件和变量的各种常见条件。 (另请注意,[[ 语法,如果它支持,则由shell处理。

对于您的情况,您不希望直接使用test,而是想要在远程主机上测试某些内容。所以尝试类似的事情:

if ssh user@host test -e "$file" ; then ... ; else ... ; fi

答案 5 :(得分:3)

ssh -q $HOST [[ -f $FILE_PATH ]] && echo "File exists"

上面将在你运行ssh命令的机器上运行echo命令。要使远程服务器运行命令:

ssh -q $HOST "[[ ! -f $FILE_PATH ]] && touch $FILE_PATH"

答案 6 :(得分:3)

测试文件是否存在:

HOST="example.com"
FILE="/path/to/file"

if ssh $HOST "test -e $FILE"; then
    echo "File exists."
else
    echo "File does not exist."
fi

相反,测试文件是否不存在:

HOST="example.com"
FILE="/path/to/file"

if ! ssh $HOST "test -e $FILE"; then
    echo "File does not exist."
else
    echo "File exists."
fi

答案 7 :(得分:1)

您可以在本地指定远程主机使用的shell。

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

小心(单)引用您希望由远程主机扩展的变量;否则变量扩展将由您的本地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
}

因此,要检查远程主机上是否存在某个文件,您可以执行以下操作:

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

答案 8 :(得分:0)

我还想检查远程文件是否存在但是使用RSH。我尝试过以前的解决方案,但他们没有使用RSH。

最后,我做了我的短功能,工作正常:

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
}

答案 9 :(得分:0)

在CentOS机器上,为我工作的oneliner bash是:

if ssh <servername> "stat <filename> > /dev/null 2>&1"; then echo "file exists"; else echo "file doesnt exits"; fi

它需要I / O重定向(作为最佳答案)以及要在远程运行的命令周围的引号。

答案 10 :(得分:-1)

这也可以:

if ssh user@ip "[ -s /path/file_name ]" ;then 
  status=RECEIVED ; 
 else 
  status=MISSING ; 
 fi

答案 11 :(得分:-1)

#its simple

if [[ "`ssh -q user@hostname ls /dir/filename.abc 2>dev/null`" == "/dir/filename.abc" ]]
then
    echo "file exists"
else
    echo "file not exists"
fi
相关问题