0

I have a directory of test files labelled 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 along with others labelled ex1, ex2, ex3, etc...

I tried to remove all files from this directory whose name doesn't include 'ex' using the following code:

rm [^a-z]

It was successful in removing all files labelled with a single digit, however files 10-12 still remain and I can't understand why.

Can someone please explain why this is happening?

Currently left in this directory are all files beginning with 'ex', and files labelled 10, 11, 12. Upon using the command once again the following error message appears:

rm: [^a-z]: No such file or directory

SLePort
  • 15,211
  • 3
  • 34
  • 44
  • `[^a-z]` matches a single character, either `^` or the characters between a and z (lowercase English). `!` is used in globbing for "not", `^` in regular expressions (inside square brackets). – cdarke Nov 10 '17 at 20:47

1 Answers1

3

Filename matching in bash is done with globs, not regexs.

But you need extglobs for what you want to do.

shopt -s extglob
rm !(ex)*
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358