0

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?

2
  • If you just want to stat the file, look at find's -exec argument. Commented Oct 12, 2011 at 17:58
  • Why is there an offtopic close vote on this question????????? Commented Oct 12, 2011 at 22:30

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

It works fine, thanks a lot. But why -> res does not work in this case?
@SpeedBirdNine: -> isn't how shell writes redirects, > is. So the - is being passed to find as an argument, which is producing an error (or would, were you not redirecting stderr to /dev/null)
@SpeedBirdNine I enhanced the post. OK?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.