2

I'm trying to find all files, that make a given command end successfully (exit code 0).

The find man page says about -exec: Execute command; true if 0 status is returned.

Although listed under Actions, I thought, that this means, that files that make the given command end with 0 will be displayed.

Past uses and trying with find . -exec true \; and find . -exec false \;, which both gave no output, prove me wrong, though.

A sample invocation would be find . -exec ./is_dir.py {} \;, which should print all found directories. (I know, that you can test for this particular case. I couldn't find a combination of tests, that satisfy my actual needs.)

Failing to formulate my problem good enough, searching yielded no results.

3

You can do:

find . -exec ./is_dir.py {} \; -o -print

It will list everything that is not a directory. Assuming that is_dir.py is executable (chmod +x is_dir.py) and contains something like:

#!/usr/bin/env python

import sys
import os

if os.path.isdir(sys.argv[1]):
    sys.exit(0)
sys.exit(1)

And if is_dir.py generates output of its own, you can do:

 rm -f nondir.lst
 find . -exec ./is_dir.py {} \; -o -exec echo  {} >> nondir.lst \;

so that its outoput doesn't get mixed with the list of names for which is_dir.py fails.

  • expr1 -o expr2, Or; expr2 is not evaluated if expr1 is true. Is found under Operators in the man page. Exactly what I was looking for. Thanks a lot. – Minix Nov 3 '14 at 13:07
  • @Minix yes, -o is for "or" – Anthon Nov 3 '14 at 13:09
  • @Minux alternatively you can adapt your script to write out a fail list and put everything in a try: except: block. – Anthon Nov 3 '14 at 13:11
  • I used a for loop in the mean time. Will compare outputs after it's done, but I'm confident they will be the same. I will mark your question after it's done running. – Minix Nov 3 '14 at 13:12
  • 1
    I try to test the things for real, that gives a higher chance I am not making typing errors. So I have a stackexchange/unix-165669-how-to-find-only-files-with-which-command-xyz-ends-successfully directory with a real ./is_dir.py and some mkdir-ed directories and some touched files. :-) – Anthon Nov 3 '14 at 13:18

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.