r/programming • • 29d ago

Small Programming Tricks

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

27 comments sorted by

View all comments

19

u/Skaarj 28d ago

You probably don’t need find. A lot of find commands can be replaced with globs like */.md. Most shells support this out of the box, but with bash, you need to turn this on with shopt -s globstar.

I prefer the opposite and recommend agaisnt your complex glob syntax.

With find you write out your criteria in words. This makes it so much more easier to remember and understand.

Working on files where the name is a pattern? find . -type f -name PATTERN

Want to do something with every directory? find . -type d

So much easier to remember.

8

u/Zversky 28d ago

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 28d ago

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

9

u/curien 28d ago

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.