SpeedBirdNine
SpeedBirdNine

Reputation: 4676

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?

Upvotes: 0

Views: 567

Answers (1)

A.H.
A.H.

Reputation: 66263

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.

Upvotes: 4

Related Questions