对于SFTP服务器上的文件存在的bash测试 [英] bash test for existence of files on SFTP server

查看:350
本文介绍了对于SFTP服务器上的文件存在的bash测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题是同一句话:不起作用 - 下文。最后一行确实没有工作 - 但我需要理解为什么倒数第二行不。我需要检查文件是否存在在远程服务器上。

有必要在以下位置检查存在的文件:

  /家庭/ remoteuser表/文件/

和当文件被处理时,它们被移动到

  /家庭/ remoteuser表/日志/存档/

想如果在存在的文件来创建一个警报 - 换句话说,该文件没有处理:

  /家庭/ remoteuser表/日志/

发现以下页面,似乎正是我要找:
http://www.cyberciti.biz/tips/find-out-if-file-exists-with-conditional-ex$p$pssions.html

测试这一点,我知道有文件存在,但不工作:

  SSH remoteuser@1.2.3.4 [! -f /home/remoteuser/logs/archive/*.*]放大器;&安培;回声&GT文件不根存在;> /home/localuser/files/dirlist.txt

由于我们知道这工作确实做本地服务器上的文件列表:

  SSH remoteuser@1.2.3.4 ls -l命令/home/remoteuser/logs/archive/*.*>> /home/localuser/files/dirlist.txt


解决方案

通配符和测试结构中的Bash

您不能使用通配符在 [命令来测试多个文件的存在。事实上,该通配符将扩大,所有文件将被传递给测试。特结果是,它会抱怨说:-f被赋予了太多的争论。

试试这个在任何非空目录查看输出:

  [! -F *。* ]

在上面的命令不会失败的唯一情况是,当只有一个相匹配的前pression文件,你的情况形式的非隐藏文件*。*在/ home / remoteuser表/日志/存档/

使用查找

一个可能的解决方案是使用查找使用grep组合:

  SSH名@服务器查找/路径/要/的/文件型的F -name\\ * \\ *。2  - ;的/ dev / null的| grep的-q。 &功放;&安培;回声非空||回声空

找到普通文件搜索(型的F),其名称形式的 的(-name),如果没有找到,则返回假的grep -q。返回1或0,如果事情被发现或没有。

The issue that I have is with the line: "does not work" - below. The last line does indeed work - but I need to understand why the second to last line does not. I need to check for file existence on the remote server.

Have a need to check for existence for files at the following location:

/home/remoteuser/files/

and when the files are processed, they are moved to:

/home/remoteuser/logs/archive/

Would like to create an alert if the files exist at - in other words, the files were not processed:

/home/remoteuser/logs/

Found the following page and seems to be what I am looking for: http://www.cyberciti.biz/tips/find-out-if-file-exists-with-conditional-expressions.html

Testing this and I know there are files there, but does not work:

ssh remoteuser@1.2.3.4 [ ! -f /home/remoteuser/logs/archive/*.* ] && echo "File does not exist in the root" >> /home/localuser/files/dirlist.txt

Because we know this works and does indeed list files on the local server:

ssh remoteuser@1.2.3.4 ls -l /home/remoteuser/logs/archive/*.* >> /home/localuser/files/dirlist.txt

解决方案

Wildcards and test construct in Bash

You cannot use the wildcards in the [ command to test the existence of multiple files. In fact, the wildcards will be expanded and all the files will be passed to the test. Te results is that it would complain that "-f" is given too many arguments.

Try this in any non empty directory to see the output:

[ ! -f *.* ]

The only situation in which the above command does not fail is when there is only one file matching the expression, in your case a non hidden file of the form "*.*" in /home/remoteuser/logs/archive/

Using Find

A possible solution is to use find in combination with grep:

ssh name@server find /path/to/the/files -type f -name "\*.\*" 2>/dev/null | grep -q . && echo "Not Empty" || echo "Empty"

find search for regular files (-type f) whose names are in the form . (-name) and return false if nothing is found, then "grep -q ." return 1 or 0 if something is found or not.

这篇关于对于SFTP服务器上的文件存在的bash测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆