Operating System - HP-UX
1833625 Members
3608 Online
110062 Solutions
New Discussion

How to remove "find: cannot open"

 
Javier_45
Occasional Advisor

How to remove "find: cannot open"

Hello guys,
I know this may sound a very silly question but whenever you are doing a find, and you don't have acess to a certain directory, you always get these kind messages:

find: cannot open ./etc/opt/...
find: cannot open ./etc/opt/resmon/...

How can I get rid of them so I will only display the files/directories that match my find?

Thanks a lot,

Javier
14 REPLIES 14
Pete Randall
Outstanding Contributor

Re: How to remove "find: cannot open"

Javier,

Pipe your find to a "grep -v cannot".


Pete

Pete
Rick Garland
Honored Contributor

Re: How to remove "find: cannot open"

If you are unable to open these areas, how do you know the find searched everywhere is could?

The messages 'cannot open' signify a permission issue. Doing the find as a non-root user.

If true, the other option is to have find skip the areas it cannot open then those messages will not appear.
A. Clay Stephenson
Acclaimed Contributor

Re: How to remove "find: cannot open"

All find's error messages go to stderr so the solution is simply:

find . -print 2>/dev/null
If it ain't broke, I can fix that.
Javier_45
Occasional Advisor

Re: How to remove "find: cannot open"

Thank you guys!
Javier_45
Occasional Advisor

Re: How to remove "find: cannot open"

Actually I still get the pesky messages with either

find . -name "*.bsf" -exec grep -v "cannot" {} \;

or

find . -name "*.bsf" | grep -v cannot

find: cannot open ./etc/opt/resmon/persistence
find: cannot open ./etc/opt/resmon/pipe
find: cannot open ./etc/opt/sanmgr
find: cannot open ./etc/opt/hpsmc
find: cannot open ./etc/sam/custom

Pete Randall
Outstanding Contributor

Re: How to remove "find: cannot open"

My mistake, Javier. I think that's because those are stderr messages, as Clay points out, and therefore not passed as stdin to grep. That's my theory anyway.


Pete

Pete
A. Clay Stephenson
Acclaimed Contributor

Re: How to remove "find: cannot open"

Of course you do. Grep is working on stdout but the error messages are going to stderr. Normally, stdout and stderr are directed to a common device (your terminal). Redirest stderr to /dev/null and the error messages will disappear.
If it ain't broke, I can fix that.
Biswajit Tripathy
Honored Contributor

Re: How to remove "find: cannot open"

As already suggested above, the "cannot open" error
messages go to stderr and pipe works on stdout, so
they still show up in your output. There are 2 solutions
to this. One, you could follow Clay's suggestion of
directing stderr to /dev/null. The other option is to
redirect stderr to stdout and then use "grep -v" to
remove the error messages.

# find . -name "*.bsf" 2>&1 | grep -v cannot

- Biswajit
:-)
Amit Agarwal_1
Trusted Contributor

Re: How to remove "find: cannot open"

REdirecting the error message to /dev/null will be faster than using grep.

$ find 2>/dev/null

this is the best way suited for your need.
Muthukumar_5
Honored Contributor

Re: How to remove "find: cannot open"

If you are using HFS file system then you can use -acl option with find to remove error messages as cannot open.. as,

find -acl '%.%, +r'

Else redirect all error messages to /dev/null simply as,

find 2>/dev/null

HTH.
Easy to suggest when don't know about the problem!
Javier_45
Occasional Advisor

Re: How to remove "find: cannot open"

Thanks a lot to all of you!
Stephen Keane
Honored Contributor

Re: How to remove "find: cannot open"

Biswajit's solution does have the benefit that any other errors (other than those containing the word 'cannot') are visible, should you wish to see them; otherwise direct stderr to /dev/null.
A. Clay Stephenson
Acclaimed Contributor

Re: How to remove "find: cannot open"

On the other hand, the grep approach also has the unwanted side effect of filtering out any pathnames which happen to contain "cannot". You would be much better served to separate stdout and stderr and if you are interested in the error messages then capture stderr to a file and process it separately.
If it ain't broke, I can fix that.
Biswajit Tripathy
Honored Contributor

Re: How to remove "find: cannot open"

A Clay Stephenson wrore:
> On the other hand, the grep approach also has
> the unwanted side effect of filtering out any
> pathnames which happen to contain "cannot".

Yes. That could, ofcourse, be fixed with couple of
extra lines of script.

> You would be much better served to separate
> stdout and stderr and if you are interested in the
> error messages then capture stderr to a file and
> process it separately.

Agreed 100%. If the programmer wrote different
things to stdout and stderr, then it was for a reason.

- Biswajit
:-)