Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

In bash, I tried

ls [a-z]*

and expected to list all the files with filename starts with small case alphabet. But why the files with name starts with big case alphabet are also shown?

>ls [a-z]*
D  e
>

In case needed, the bash version:

bash --version GNU bash, version 4.1.5(1)-release (i486-pc-linux-gnu) Copyright (C) 2009 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
share|improve this question
    
* in bash isn't a regex *. [a-z] looks a bit regex like and bash seems to accept that. ls a+ won't match aaa for example, so if it does support regex, it does barely. i'm not sure to what extent if at all. echo a{3} , echo a\{3\} just echoes a{3} and doesn't interpret it like a regex. –  barlop Mar 1 '11 at 13:51
    
here is an article on bash pattern matching faqs.org/docs/bashman/bashref_35.html it mentions "If the extglob shell option is enabled using the shopt builtin, several extended pattern matching operators are recognized. In the following description, a pattern-list is a list of one or more patterns separated by a `|'. Composite patterns may be formed using one or more of the following sub-patterns:" But i'm not sure how you'd enable that option –  barlop Mar 1 '11 at 13:59
    
On cygwin, [a-z]* looks like it does exactly the same as [[:lower:]]* they don't display uppercase. GNU bash, version 3.2.51(24)-release (i686-pc-cygwin) –  barlop Mar 1 '11 at 14:02
1  
@barlop: The behavior of character ranges depends on the locale, specifically the LC_COLLATE setting In some (many?) locales, [a-z] includes "aAbB...yYz" but not "Z". You can enable extended globbing using shopt -s extglob. I get different results for [a-z] and [[:lower:]] on Cygwin. –  Dennis Williamson Mar 1 '11 at 14:44
add comment

2 Answers

up vote 3 down vote accepted

It's not regexp, which is where the confusion comes in. Instead, it uses a system called "globbing" - see:

$ man 7 glob

Instead of [a-z] you need to use [[:lower:]] as such:

$ ls -d [[:lower:]]*
share|improve this answer
    
is "globbing" the so called "pathname expansion"? –  zhanwu Mar 1 '11 at 13:59
    
No - pathname expansion is such things as ~fred/foo.bar translating into /home/fred/foo.bar –  Majenko Mar 1 '11 at 16:17
add comment

1) It may depend on your operating system, more specifically your filesystem. Is your filesystem case sensitive? You don't mention OS, or filesystem type.

2) bash also can do case insensitive globbing. You can check to see if this is your current case with shopt | grep nocaseglob

Also, as an aside, remember that ls isn't doing anything with the files. Your shell is, then is passing the filenames to ls.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.