Ananya
Ananya

Reputation: 1

What is the command for listing all files ending in small letters?

suppose i have 'n' number of files such as apple,BAT,comas,aba,abc,abc03.....etc

also What is the command for listing all files which end in small letters but not ‘a’ and ‘c’? or any specific character

Upvotes: 0

Views: 4438

Answers (2)

thkala
thkala

Reputation: 86383

On bash:

$ LC_ALL=C
$ ls
bar  BAT  cab  foo  ieee2000  MAC  moc  test  zac  zara  ZOO
$ ls *[a-z]
bar  cab  foo  moc  test  zac  zara
$ ls *[bd-z]
bar  cab  foo  test
$ ls *[^cC]
bar  BAT  cab  foo  ieee2000  test  zara  ZOO
$ ls *[^bc]
bar  BAT  foo  ieee2000  MAC  test  zara  ZOO

Since these are shell expansions you can also use them in loops etc relatively easily.

Note the LC_ALL=C setting - if you use a non-English locale it may be required in order to produce correct results:

$ echo $LC_COLLATE
en_US.UTF-8
$ ls *[a-z]
bar  BAT  cab  foo  MAC  moc  test  zac  zara  ZOO
$ LC_COLLATE=C
$ ls *[a-z]
bar  cab  foo  moc  test  zac  zara

As seen in the sample above you can set the more specific LC_COLLATE variable instead of LC_ALL.

EDIT:

By the way, at least on my system (Mandriva Linux 2010.1), the locale also affects grep:

$ LC_COLLATE=en_US.UTF-8
$ echo A | grep '[a-z]'
A
$ LC_COLLATE=C
$ echo A | grep '[a-z]'
$ 

Upvotes: 3

Santiago Alessandri
Santiago Alessandri

Reputation: 6855

You can use a combination of ls and grep with regular expressions:

ls | grep -e "[bd-z]$"

Upvotes: 0

Related Questions