this post was submitted on 30 Jun 2026
51 points (100.0% liked)

Linux

14231 readers
713 users here now

A community for everything relating to the GNU/Linux operating system (except the memes!)

Also, check out:

Original icon base courtesy of lewing@isc.tamu.edu and The GIMP

founded 3 years ago
MODERATORS
 

I came across this older article from 2020 and I found it informative. It's about how the shell does globbing and the potential issues it can cause if not understood correctly.

TLDR:

find . -not -name *.py -delete and find . -not -name '*.py' -delete will behave differently in certain scenarios.

In the first example, the shell will replace the wildcard pattern with a list of matching file names IF there are any matches in the current directory. If there isn't, then it won't do anything and will pass *.py to find.

In the second example, the shell won't do any globbing at all and will just pass *.py

top 9 comments
sorted by: hot top controversial new old
[–] vk6flab@lemmy.radio 22 points 1 week ago (2 children)

Basic habit to get into:

If you're contemplating doing something destructive, do a dry-run first.

In this case, remove the -delete flag, run the command and see what you get.

A good approach is to build a command step by step and test your assumptions each iteration.

Things might take a few moments longer, but one day it's going to save your bacon.

As for unexpected globbing, learn the difference between quoted and unquoted, and single versus double quotes.

Source: Linux user for 25+ years

[–] unglueclass23@programming.dev 4 points 1 week ago (2 children)

What's the difference between single vs double quotes?

[–] IanTwenty@piefed.social 11 points 1 week ago* (last edited 1 week ago)

Presuming BASH:

https://www.gnu.org/software/bash/manual/bash.html#Quoting

Single quotes are much safer/predictable because:

Enclosing characters in single quotes (‘'’) preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.

Not to say they are always the right choice though.

[–] lagoon8622@sh.itjust.works 6 points 1 week ago

The shell doesn't expand or interpolate single-quoted strings, they are literals

[–] supersquirrel@sopuli.xyz 1 points 1 week ago* (last edited 1 week ago)

If you're contemplating doing something destructive, do a dry-run first.

@vk6flab@lemmy.radio is a WITCH I tell you! This is black magic!!

[–] p_consti@lemmy.world 3 points 1 week ago (2 children)

Using zsh will gelp with that mistake, because by default it will turn the 'no matches' scenario into an error. But seriously, always quote your arguments if there is any chance for ambiguity (also for variables)

[–] unglueclass23@programming.dev 2 points 1 week ago

I use bash and it did throw that error, however the find . | egrep *.py example at the end did not.

[–] fruitcantfly@programming.dev 1 points 1 week ago

It took a while to get used to zsh bailing on non-matching wildcards, but it honestly makes a lot more sense

[–] toynbee@piefed.social 2 points 1 week ago

Don't forget backslashes as an option.