check if file exists on remote host with ssh
I would like to check if a certain file exists on the remote host. I tried this:
$ if [ ssh reg@localhost -p 19999 -e /home/reg/Dropbox/New_semiosNET/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")
这是一个简单的方法:
if ssh $HOST stat $FILE_PATH > /dev/null 2>&1
then
echo "File exists"
else
echo "File does not exist"
fi
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
不能比这更简单:)
ssh host "test -e /path/to/file"
if [ $? -eq 0 ]; then
# your file exists
fi
链接地址: http://www.djcxy.com/p/97126.html
上一篇: 获取后台进程的退出代码
下一篇: 用ssh检查远程主机上是否存在文件