3

I was practicing the use of wildcards today .. that was a lot of fun.
The most complex thing that worked out exactly as I was expecting was:
ls [![:digit:]]*[a-z][0-9][0-9][0-9][aA-zZ]*[![:digit:]]

But I didn't actually manage to exclude a string.
How can I list only files which do not contain "test"?
Here some examples what I've tried already:

ls *!("test")*
ls !("test")
ls !=*"test"*
ls !(*"test"*)
ls *^test*
ls *(^test)*
ls (^test)*
ls !test*
ls !*test*
ls *!test*
ls !{test}
ls !*{test}*
ls *!{test}*
2
  • To get appropriate answers, you should specify which shell you are using Commented Dec 28, 2020 at 21:13
  • GNU bash, version 5.0.17 Commented Dec 28, 2020 at 21:42

2 Answers 2

4

In Bash:

$ shopt -s extglob
$ touch a b c test
$ ls !(*test*)
a  b  c
3
  • Make it !(*test*) to exclude anything with test in the front, middle or end. !(*test) would only exclude names where test appears at the end. Commented Dec 28, 2020 at 21:12
  • @ilkkachu: thx, applied Commented Dec 28, 2020 at 21:13
  • Thanks man, that finally worked! Even with combined lower + upper case option ls !(*[Tt][Ee][Ss][Tt]*) Commented Dec 28, 2020 at 21:39
1

Alternatively and portably you can use find command as following:

find -type f ! -name '*test*'

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.