Bash Wildcards – How to Exclude One Pattern from Glob Match

bashwildcards

I have several files with the same base filename. I'd like to remove all but one

foo.org #keep
foo.tex #delete
foo.fls #delete
foo.bib #delete
etc

If I didn't need to keep one, I know I could use rm foo.*.

TLDP demonstrates ^ to negate a match. Through trial and error, I was able to find that

rm foo.*[^org]

does what I need, but I don't really understand the syntax.

Also, while not a limitation in my use case, I think this pattern also ignores foo.o and foo.or. How does this pattern work, and what would a glob that ignores only foo.org look like?

Best Answer

shopt -s extglob
echo rm foo.!(org)

This is "foo." followed by anything NOT "org"

ref: https://www.gnu.org/software/bash/manual/bashref.html#Pattern-Matching

Related Question