r/programming • • Sep 14 '26

Small Programming Tricks

https://will-keleher.com/posts/small-programming-tricks-matter/
179 Upvotes

27 comments sorted by

View all comments

Show parent comment

7

u/Zversky Sep 15 '26

Yes, but to be fair, grep exit **/*.py is much faster to write than find . -type f -name '*.py' -exec grep -H exit {} \;. And the latter also includes dot-directories which I cannot remember how to ignore.

9

u/smcameron Sep 15 '26

I never use the {} notation, and find it hard to remember. Instead I pipe to xargs, e.g.:

find . -type f -name '*.py' | xargs grep exit

8

u/curien Sep 15 '26

Files with weird names (spaces etc in the filename) will break that. It's better to use find ... -print0 | xargs -0 ... if you don't want to use find ... -exec.

2

u/Skaarj Sep 15 '26

Yes the grep and glob variant is shorter to write. But I find that less important than the ease of remembering.

2

u/Zversky Sep 15 '26

Find is rarely used alone. I had to man grep to find that -H switch, which is implied when you use it on multiple files.

3

u/curien Sep 15 '26

If you terminate the exec with + instead of ; then it passes multiple files per command, just like the glob.

2

u/knome Sep 16 '26

you'll still want -H in case the last invocation only has a single file.

2

u/curien Sep 16 '26 edited Sep 16 '26

To be really sure, you need it either way because there could only be one matching file. And f the find uses multiple commands due to exceeding ARG_MAX, won't the glob version just fail?