简体   繁体   中英

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 / -name 'test2abc' 2>/dev/null -> res

The file res is empty. 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. Is there a way to use juts the stat command to search for the file in the subfolders as well?

The -> res part should be > res only.

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.

It may be wise not to suppress error messages. A simple way can be redirecting both stderr and stdout to the file like this:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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