简体   繁体   English

无法将find命令的结果保存在文件中-文件为空

[英]cannot save the results of find command in a file - file is empty

The program is supposed to find a file and return whether it exists on the system or not, now i have found that find command should be used, but since this command will be initiated by the code (using System) i need to save the results in a file, and trying on the terminal, i cannot get it to work, the result doesn't appear in the file, i am trying: 该程序应该找到一个文件并返回它是否存在于系统上,现在我发现应该使用find命令,但是由于该命令将由代码启动(使用系统),因此我需要保存结果在文件中,并在终端上尝试,我无法使其正常工作,结果未出现在文件中,我正在尝试:

find / -name 'test2abc' 2>/dev/null -> res

The file res is empty. 文件res为空。 How to do it right? 怎么做对?

Also is there a better way to do it, i am supposed to print the details of the file using stat command if the file exists. 还有一种更好的方法,如果文件存在,我应该使用stat命令打印文件的详细信息。 Is there a way to use juts the stat command to search for the file in the subfolders as well? 有没有一种方法可以使用juts stat命令在子文件夹中搜索文件?

The -> res part should be > res only. -> res部分应该是> res

If you try the command like this on the commandline: 如果您在命令行上尝试这样的命令:

find / -name 'test2abc' -> res

it will print an error: 它将显示错误:

find: paths must precede expression: -

The - is not part of any valid redirection and hence given to find which cannot interpret it either. -不是任何有效重定向的一部分,因此可以find哪个也不能解释它。

It may be wise not to suppress error messages. 不禁止显示错误消息可能是明智的。 A simple way can be redirecting both stderr and stdout to the file like this: 一种简单的方法是将stderr和stdout都重定向到该文件,如下所示:

find / -name 'test2abc' > res 2>&1

Then the error about the - would have been in the file right from the start you you would have known what`s wrong very fast. 然后,关于-的错误从一开始就已经存在于文件中,您很快就会知道出了什么问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM