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?
rm foo.*[^org]
removes all files which last character is neithero
,r
org
, sofoo.foo
wouldn't match either.Regular Expression
. You should be careful with your grouping characters. By using brackets you've specified a character class meaning that you would delete any files that had an extension with the letterso
,r
org
in any order. Use parenthesis to create a group and preserve the order of characters.foo.*[^org]
will match any filename that begins withfoo.
with one or more characters after the dot where the last character is not o, r, or g. So it would matchfoo.orb
, but notfoo.org
orfoo.or
orfoo.o
. GlennJackman's answer shows how to enable extended pattern matching features to negate a match.