3

Hi previous questions on this topic contain answers for Linux but does not work for Solaris 10.

find . ! -readable -prune

Does not work in solaris since -readable is not POSIX.

What is the POSIX compliant command that exclude all “permission denied” messages from “find” in Solaris?

Correct answers: jlliagre and random832 gave correct answers.

  • You are trying to not to print them? – Braiam Aug 9 '15 at 20:18
  • 1
    Those errors are given on STDERR, a quick and dirty hack would be to redirect STDERR to /dev/null ... find . -prune 2> /dev/null. However any other errors will be lost too. – Drav Sloan Aug 9 '15 at 20:19
  • Sorry your command doesn't work: find: bad option 2 find: [-H | -L] path-list predicate-list – he wei Aug 9 '15 at 22:34
  • 1
    @hewei Are you using (t)csh? Don't use (t)csh. – zwol Aug 9 '15 at 23:04
  • @hewei If you must use csh, the (somewhat, if you want it in a file you need to put the file instead of /dev/tty) equivalent command is (find . -prune > /dev/tty) >& /dev/null. – Random832 Aug 9 '15 at 23:15
6

Here is a POSIX way to prune any non readable directory with find :

find . \( -exec sh -c ' if [ ! -r "$1" ] ; then { exit 1 ; } ; else for i in "$1"/* ; do if [ -d "$i" -a ! -r "$i" ]; then exit 1; fi; done; fi ' sh {} \; -o -prune \) -a -print

Note that if this is a full Solaris installation, GNU grep is available in /usr/sfw/bin/ggrep.

  • This does not work: wrds(/)% find . ( -exec test ! -r {} \; -prune ) -a -print find: cannot read dir ./lost+found: Permission denied find: cannot read dir ./var/lost+found: Permission denied – he wei Aug 9 '15 at 22:39
  • -a -print should be -o -print, I believe. – zwol Aug 9 '15 at 23:03
  • I am in ksh , and your first command find . ( -exec sh -c 'if [ ! -r "$1" ] ; then { exit 1 ; } ; fi ' sh {} \; \ -o -prune ) -a -print yields: " find: cannot read dir ./var/sadm/pkg/SMCvim/save: Permission denied find: cannot read dir ./var/sadm/pkg/SMCnedit/save: Permission denied ........" – he wei Aug 9 '15 at 23:35
  • Indeed, I just modified my answer to fix this, I hope. – jlliagre Aug 10 '15 at 0:30
  • It works indeed, thanks. One downside of ksh is that the arrow keys don't work. For exmaple, uparrow yields ^[[A on Putty – he wei Aug 10 '15 at 0:39
2

Just filter them out.

find . 2>&1 | grep -v "^find: cannot read dir .*: Permission denied$"
  • I'm sorry it doesn't work on SunOS wrds3 5.10 Generic_150400-23 sun4u sparc SUNW,SPARC-Enterprise. The results are: wrds(~)% find . 2>&1 | grep -v "^find: cannot read dir .*: Permission denied$" Illegal variable name. – he wei Aug 9 '15 at 22:27
  • 2
    @hewei Use ksh instead of csh for your shell. – Random832 Aug 9 '15 at 23:12
0

If you would prefer not to invoke a subshell for every single file, this refinement of jlliagre's answer does it all with find predicates, as long as there are no ACLs to make it even more complicated. Correctly handling directories that are readable but not searchable is left as an exercise. If you haven't seen the #! thingy before, it means "don't try to copy all this onto the command line; instead, put it in a file, verbatim, chmod +x it, and invoke it as ./nameoffile <directory>."

#! /usr/xpg4/bin/sh

PATH=/usr/local/bin:/usr/xpg4/bin:/usr/bin:/bin
export PATH

me="-user $(id -u)"
my_groups="( -group $(id -G | sed 's/ / -o -group /g') )"

prune_me="( ( $me ! -perm -0400 ) -prune )"
prune_group="( ( ! $me $my_groups ! -perm -0040 ) -prune )"
prune_other="( ( ! $me ! $my_groups ! -perm -0004 ) -prune )"

find "$@" $prune_me -o $prune_group -o $prune_other -o -print
  • Thanks. It doesn't work as it says " wrds(~)% mysearch.sh / id: illegal option -- u Usage: id [-ap] [user] id: illegal option -- G Usage: id [-ap] [user] find: cannot find ! name " Would you have a script that would allow me to grep a keyword , for example 'Carhart' inside all .sas files in all subdirectories, I tried something like the following but doesn't work: (find . -name '*.sas' -prune -type f -exec grep 'Carhart' > /dev/tty) >& /dev/null – he wei Aug 9 '15 at 23:38
  • Try with the two lines I just added. For the grepping of a keyword, please ask a new question. We strongly prefer one and only one question per question. – zwol Aug 10 '15 at 0:10
  • It no longer shows id error, but it shows "bash-3.2$ find: cannot read dir /var/sadm/pkg/SUNWrda/save: Permission denied bash: find:: command not found bash-3.2$ /var/sadm/pkg/SUNWrda/pkginfo bash: /var/sadm/pkg/SUNWrda/pkginfo: Permission denied bash-3.2$ /var/sadm/pkg/SMCiperf bash: /var/sadm/pkg/SMCiperf: is a directory .........." – he wei Aug 10 '15 at 0:22
  • I need to see the output of ls -l /var/sadm/pkg/SUNWrda please. Edit it into your question - don't put it in the comments here. – zwol Aug 10 '15 at 0:26
  • @hewei OK: my answer should have worked on that. I cannot help you any further. – zwol Aug 10 '15 at 0:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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