Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am using cygwin to find a file on the cygdrive.

However I need to suppress the permission denied messages (otherwise the results get hidden in the error messages). The following command works:

find -name 'myfile.*' |& grep -v "Permission denied"

I don't understand why the ampersand needs to be put into this command, would have expected this to work but it doesn't.

find -name 'myfile.*' | grep -v "Permission denied"

Please explain the meaning of the ampersand.

share|improve this question

3 Answers 3

up vote 6 down vote accepted

In Unix-like systems, there are two output paths that if left unmodified will send output to your screen. Standard error (or stderr) is the one that captures most failures and error conditions.
To pass the permission denied message in the stderr to the same output stream as "regular output" you must combine the two. In your example, in order for your grep -v to properly operate on it, you combine stdout (standard output) and stderr with the arcane syntax you see.

From the advanced bash scripting guide :

If ‘|&’ is used, the standard error of command1 is connected to command2's standard input through the pipe; it is shorthand for 2>&1 |. This implicit redirection of the standard error is performed after any redirections specified by the command.

Also, as geirha points out, if you want to just gid rid of stderr output, you would want to do something like

find -name 'myfile.*' >2 /dev/null

or perhaps

find -name 'myfile.*' > /tmp/errorlog

And note that if you have strings of commands, such as a find passing its output to xargs you would need to put the entire pipeline of commands in parentheses to capture the output from all components of the command. E.g.,

 (find | egrep ^[RS].[0-9]+/.svg] | xargs head -1 )  2> /dev/null

If you left out the parentheses, and did this instead --

 find | egrep ^[RS].[0-9]+/.svg] | xargs head -1 2> /dev/null 

you would still see permission denied errors from the find or egrep, but stderr would be redirected for xargs.

As you've seen, you would likely throw away the stderr only after viewing its contents during a test run.

Note that with GNU find and as far as I can tell, any POSIX-compliant find, the -print option is implicit. You can still supply it explicitly if you like.

share|improve this answer

Error messages are written to stderr, not stdout. |& redirects stderr

share|improve this answer

If you want to ignore the error messages, just redirect stderr to /dev/null.

find . -name 'myfile.*' -print 2>/dev/null

Also, consider reading http://mywiki.wooledge.org/UsingFind.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.