Why glob `* [! T] * `returns files with names containing` t`s?

I don't really know, after reading the glob (programming) of the results printed by the following command in the shell, I am 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 echo

will be expanded to match any filenames that do not contain the letter t, but the result was exactly the opposite, why?

This command prints out all filenames that contain the letter t:

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

      

In the previous command, I simply denied [t]

before [!t]

, then in my expectation he should have done the opposite of the second command.

+3


source to share


3 answers


This globe:

echo *[!t]*

      

will find any filename in which at least one character is not t

.

So, if you have file names such as t

, tt

, ttt

, then the file names will not be listed with this glob.



Decision:

If you want to specify filenames that do not have a letter t

, you can use this command find

:

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

      

You can also add -type f

for file listing -type d

only or for directory listing only.

+4


source


!

is the standard negation character for a parenthesis expression. *[!t]*

means a match of zero or more arbitrary characters, followed by anything other than t

, followed by zero or more arbitrary characters. In other words, match any filename containing a different character that t

. Something that will not fit, this is the file names that consist only of t

s: t

, tt

, ttt

etc.



If you only want to combine filenames that don't contain t

, see Charles Duffy's answer when he beat me.

+4


source


Like other answers, it *[!t]*

returns files with any character t

.

What they haven't provided yet 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

+4


source







All Articles