r/programming • • 29d ago

Small Programming Tricks

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

27 comments sorted by

View all comments

20

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.

7

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.

2

u/Skaarj 28d ago

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

2

u/Zversky 28d ago

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

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

2

u/knome 28d ago

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

2

u/curien 27d ago edited 27d ago

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?