Original find which works in OS X but not now in Ubuntu 16.04

find $HOME -type f -name "*.tex" -exec grep -l "masi" {} + |  vim -R -

(simplified version still; you can omit vim part but it just shows here that I am handling the results)

In Ubuntu 16.04, I get those permission denied -messages. I want to exclude permission denied messages from my find; actually those files seem to be mostly .dotFiles

drwx------  2 root root  4096 touko 29 23:59 .gvfs

I cannot combine the code to my find of searching. So find all but exclude those files with permission denied (! -readable -prune). Pseudocode

find $HOME -type f -name "*.tex" \ 
  -o ! -readable -prune -o \    
  -exec grep -l "masi" {} + 

but it seems to expand too much by giving a massive list etc messages

grep: /home/masi/.conda/envs/my_root/src/linux-headers-4.2.0-27/include/linux/power: Is a directory`

So different than the thing works in OS X. I tried also unsuccessfully -perm a+r instead of the parsing structure but I still get those permission denied -messages.

Reviewing Gilles' answer

My final solution is

find $HOME +perm 0666 -type f -name ...

because readable is not POSIX and does not work in OSX.

How can you combine search and exclude in find?

  • The easy answer is to simply filter out standard error (... 2> /dev/null). – DopeGhoti Jun 1 '16 at 23:53
  • Yes, it is easy but I want to catch my errors, not throw them away. – Léo Léopold Hertz 준영 Jun 1 '16 at 23:54
  • 2> find.err, then you can review the error log at your leisure. – DopeGhoti Jun 1 '16 at 23:54
  • 1
    I feel it appropriate to add here that if you need to add some other search criteria, that should be done with -o: find . ! -readable -prune -o -name '*.txt' is one of the comments on the answer you have accepted there... and it has 33 upvotes so really, you can't miss it... – don_crissti Jun 1 '16 at 23:56
  • alternatively: find [...] 2> >(grep -v 'permission denied') will filter out those specific errors from standard error. – DopeGhoti Jun 1 '16 at 23:58

The answer to the question you asked has to do with boolean algebra and operator precedence. Your find command reads:

  • If it's a regular file, and it's name matches *.tex, then do nothing.
  • Otherwise, if it isn't readable, skip it without recursing.
  • Otherwise run grep.

Put the readability test first. This will cause the tests matching the files you're interested in to be attached to the command you want to run on them.

find ~ ! -readable -prune -o \    
       -type f -name "*.tex" -exec grep -l "masi" {} + 

Alternatively, since you seem to be on Linux and OSX, use the recursion facilities of GNU grep.

grep -s -r --include='*.tex' -l 'masi' ~

You shouldn't have unreadable files in your home directory. Having files belonging to root indicates that you ran some programs as root (probably through sudo) and they left some automatically-generated state files or temporary files behind.

~/.gvfs is the root of the GVFS mounting point hierarchy, and it definitely should belong to you, not to root, otherwise GVFS mounts that you try to do (of removable media, network drives, etc.) won't work. Run sudo rmdir ~/.gvfs. If you can't delete it because it's still in use, move it out of the way (sudo mv ~/.gvfs~/.gvfs.root`) and get rid of it when what's currently mounted there gets unmounted.

  • I cannot understand why you cannot reverse -! -readable -prune and -type -f -name ... like -type f -name...and boolean Algebra -o ! -readable -prune. I do understand Mathematics but here I am confused. Is -o XOR or AND or OR? How do you read it? I really want to use the basic boolean logics. Why does the order matter there? – Léo Léopold Hertz 준영 Jun 2 '16 at 6:22
  • @Masi Ok, maybe your confusion isn't with the logic but with the evaluation strategy. -prune and -exec are both actions. They apply when they're evaluated, and the boolean operators (juxtaposition for AND, -o for OR) are short-circuiting, meaning that the right-hand side is only evaluated if the left-hand side is true (for AND), resp. false (for OR). Thus -name '*.tex' -o … -o -exec … only executes the command on files for which both the -name '*.tex' and the middle condition are false. – Gilles Jun 2 '16 at 10:14
  • 1
    @Masi Think of juxtaposition as “if … then …” and of -o as “if not … then …”. – Gilles Jun 2 '16 at 10:15
  • Command ! -readable -prune says if not readable do not go. Continue -type f ... says `then do this. I extended the OS X part here apple.stackexchange.com/q/240387/15504 – Léo Léopold Hertz 준영 Jun 2 '16 at 11:30

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.