Home > OS >  How do I reject a character in bash?
How do I reject a character in bash?

Time:02-18

I'll give two examples:

Example 1,

touch {a..z} 
ls
a b c d e f g h i j k l m n o p q r s t u v x w y z

but I want to reject n

a b c d e f g h i j k l m "" o p q r s t u v x w y z

Example 2, (what I really want to do)

ls
c1 c-2 ca4 cA5 cs1 cs2 cs3 cs4 cs5
rm c* #but not starting with "cs"

With example 1 I'm not trying to be ambiguous, I want something like:

rm c*(!s) #this command will give an error, but I want to remove all strings starting with "c", not "cs"

How do I reject a character?

CodePudding user response:

You seem to be asking for a solution in the area of pattern matching, so a negated character class fits the request pretty well:

rm c[^s]*

The [^s] matches exactly one character other than s (including uppercase S), so the overall pattern will match

c1, c2, ca, clover, c12, cS, c@:,., etc,

but not

c, cs, cs1, cs101, or csience.

You can find more details in the Bash manual.

  • Related