?
Solved

Unix - ls [A-Z] does not work

Posted on 2005-04-06
14
Medium Priority
?
165 Views
Last Modified: 2008-02-01
Have 3 different Linix machines all haves names a b c and A B C

On one machine:  ls [A-Z] will list just A B C
but the other two it will list A B C a b c

I assume its something to do with the ascii table set up please help!!!!
0
Comment
Question by:philmccrackin69
  • 6
  • 5
  • 3
14 Comments
 
LVL 12

Expert Comment

by:andrewjb
ID: 13718782
It possibly depends upon the shell you're using... On my fedora server:

ls [a-z] shows me the files 'a' and 'A' in sh and bash, but not csh
ls [A-Z] only shows 'A' in all of them.

.. but I'd be interested to know why [a-z] and [A-Z] give different results !
0
 
LVL 6

Expert Comment

by:bmedward
ID: 13726485
Are you really trying to find
     [A-Z]                    # Any single upper-case ascii alphabet letter
or
     [a-zA-Z]               # Any single upper or lower case ascii alphabet letter
or
     [:alpha:]              # Any single upper or lower case ascii alphabet letter
or something entirely different?

And, what shell are you using? Do you have any aliases enabled that change the behavior of ls?
0
 

Author Comment

by:philmccrackin69
ID: 13726847
All three systems are running bash shell.

I type:
ls [A-Z]

This list files A B C as it should!!!  on one machine (SUN OS)

But lists files A B C AND!! a b c on the other two machines (one running Mandrake 8.1 the other Fedora Core 2)

Note: ls [A-Z]* lists all files and contents of directories (same as ls *) on the second two machines

Aliases : ls --> 'ls --color=tty'

...Thanks for your help
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 12

Expert Comment

by:andrewjb
ID: 13726989
There's a shell option called nocaseglob in bash, which affects it (decides whether the pattern matching is case sensitive or not)

Try 'shopt nocaseglob' on each system and see whether they're different.
0
 

Author Comment

by:philmccrackin69
ID: 13727038
All Return:
nocaseglob      off

Cheers
0
 
LVL 6

Expert Comment

by:bmedward
ID: 13727141
Does it match the same way when you log in locally (/dev/console) as remotely (/dev/ttypn)?
How about if you change your terminal definition on /dev/console manually to VT100 (export TERM=vt100)?
Does it behave the same in a subshell/script as it does on the command line?
0
 
LVL 12

Assisted Solution

by:andrewjb
andrewjb earned 120 total points
ID: 13727166
There are a couple of other options, too. (Have a look a the (long!) bash man page.)

Try just 'shopt' and see what, if anything, is different. Maybe try 'locale' as well, since that tells bash what collation order to use for alphabet..

A.
0
 
LVL 6

Expert Comment

by:bmedward
ID: 13727862
Slightly off topic, is "ls [A-Z]" really what you want to do? For instance, if you have a subdirectory that begins with a capital letter, it will not be listed - while all of its contents will be listed reguardless of case. Piping the output to grep would not be much more expensive for most situations. I would be curious to know if grep treats the match the same way that ls does.

Does it work as:   ls | grep "[A-Z]"

0
 

Author Comment

by:philmccrackin69
ID: 13727931
Thanks for the grep idea but i've alreay tried this and it lists everything ie. A B C and a b c

However ls [ABCDEFGFIJKLMNOPQSRTUVWZYX] works fine, as does ls | grep [ABCDEFGFIJKLMNOPQSRTUVWZYX]

So its something to with the [A-Z] range its a real puzzler and really annoying

Thanks for your help so far...
0
 
LVL 6

Expert Comment

by:bmedward
ID: 13727947
oops. not enough caffeen.
that example will successfully match anything with a capital letter in it.
instead, try :    ls | grep "^[A-Z]"
     this will only match lines that start with a cap.
0
 
LVL 6

Accepted Solution

by:
bmedward earned 400 total points
ID: 13728009
do you have any of the following environmental variables set?
LC_ALL
LC_COLLATE
LANG

Grep's manpage claims that they are used to determine range interpretation, not all of my systems use them.
I have one that has
LC_COLLATE=C
LANG=en_US
0
 

Author Comment

by:philmccrackin69
ID: 13728022
Both:
ls | grep "[A-Z]" and ls | grep "^[A-Z]" both still match a b c

So it is definatly something with [A-Z]
0
 

Author Comment

by:philmccrackin69
ID: 13728074
LANG is set to en_GB.UTF-8

But others are not!!


Just tried setting LC_COLLATE=C works a treat... all is once again well

Thought i was going mad for a second!!!!

Thanks so much!!!!

Out of intrest do you know what LC_COLLATE is used for and if their are any other options?

This is my first time on Expert Exchange do i click accept on your solution now?

Thanks so much to all you guys!!!
0
 
LVL 6

Expert Comment

by:bmedward
ID: 13728200
This might help explain it, but I doubt it.   http://bama.ua.edu/cgi-bin/man-cgi?environ+5 
Gotta love the cryptic explinations of man pages. Honestly, I have never closed out a question.

I would recommend this section of the FAQ http://www.experts-exchange.com/Miscellaneous/help.jsp#hi9. I would also recommend a split, andrewjb had some very good contributions to this thread.

Welcome to EE, and good luck.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Originally published Entrepreneur.com Booming numbers of freelancing professionals are changing the face of work. In the United States alone last year, the number of workers freelancing grew from 700,000 to 54 million, according to a Freelancers’…
Each password manager has its own problems in dealing with certain websites and their login methods. In Part 1, I review the Top 5 Password Managers that I've found to be the best. In Part 2 we'll look at which ones co-exist together and why it'…
Notifications on Experts Exchange help you keep track of your activity and updates in one place. Watch this video to learn how to use them on the site to quickly access the content that matters to you.
Saved searches can save you time by quickly referencing commonly searched terms on any topic. Whether you are looking for questions you can answer or hoping to learn about a specific issue, a saved search can help you get the most out of your time o…
Suggested Courses

579 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question