GIZ
GIZ

Reputation: 4643

Why does the glob `*[!t]*` return files whose names contain `t`s?

I have really no idea after reading glob (programming) of the results printed by following command in shell, I'm using (bash) as my shell. Given this directory hierarchy:

/sub2
    s.py
    t2.txt
    nametoname.txt
    bees.txt
    /sub22

$ echo *[!t]*
bees.txt nametname.txt s.py sub22 t2.txt 

In my understanding the arguments to echo will be expanded to match any filenames that don't contain the letter t, but the result was quite the opposite, why?

This command outputs all filenames that contain the letter t:

$ echo *[t]*
nametname.txt t2.txt

In the previous command I just negated [t] to [!t], then in my expectation it should do the opposite of the second command.

Upvotes: 3

Views: 61

Answers (3)

anubhava
anubhava

Reputation: 785481

This glob:

echo *[!t]*

Will find any filename that at least one non-t character in it.

So, If you have filenames as t, tt, ttt then those filenames won't be listed using this glob.

Solution:

If you want to list filenames that don't have letter t in it then you can use this find command:

find . -maxdepth 1 -mindepth 1 -not -name '*t*'

You may also add -type f for listing files only or -type d for listing directories only.

Upvotes: 4

chepner
chepner

Reputation: 531718

! is the standard character for negating a bracket expression. *[!t]* means match zero or more arbitrary characters, followed by anything except a t, followed by zero or more arbitrary characters. In other words, match any file name that contains a character other that t. What it won't match are file names consisting only of ts: t, tt, ttt, etc.

If you only want to match filenames that don't contain any t, see Charles Duffy's answer, as he beat me to it.

Upvotes: 4

Charles Duffy
Charles Duffy

Reputation: 295619

As other answers have given, *[!t]* returns files with any non-t character.

What they haven't yet provided is a workaround:

shopt -s extglob  ## enable extglobs
echo !(*t*)       ## list files with names not containing t

See http://wiki.bash-hackers.org/syntax/pattern#extended_pattern_language

Upvotes: 4

Related Questions