Listing files ends with small letters

January 13, 2011 at 00:58:09
Specs: Linux i686
I am beginer to shell scripting and this is homework for me .
I tried ls *[a-z] but it gives list of files which are ends with capital letter and small letter.
how to filter out only small letter ending files

I have found answer in

http://www.computing.net/answers/un...

but it uses pipe concept with egrep command

I want it with plain ls pattern matching
Thanks in advance

OS: kubuntu 9.04


See More: Listing files ends with small letters

Report •

#1
January 13, 2011 at 01:26:17
You are doing something wrong. ls *[a-z] should do what you require. (Although I was absolutely sure of this, I just tried it on a FreeBSD machine to confirm.) What flavour of UNIX are you using?

Report •

#2
January 13, 2011 at 10:33:24
I am not near a Linux box, but this works on my Solaris 9 with korn shell:

ls | grep [a-z]$

This uses grep's regular expression, $, that my ls command doesn't support. I'll try it tonight on my Linux box. (some systems may need to use the extended grep: egrep).

ls *[a-z]

on my system not only gets files ending with a lower case letter, but files beginning with a lower case letter.


Report •

#3
January 13, 2011 at 12:10:57
Just tried the command on a Solaris box and I see that you are correct. But

ls --hide=*[!a-z]

seems to do the trick.


Report •

Related Solutions

#4
January 13, 2011 at 16:13:04
I've been doing pattern matching for years and never encountered this issue. On AIX, I created the files in his original post and then executed the:

ls *[a-z] command.

It returned:

chapa
chapb
chapc
chapd

It works fine on AIX.


Report •

#5
January 13, 2011 at 23:57:44
And on Linux and FreeBSD - but not on Solaris!

Report •

#6
January 14, 2011 at 07:47:36
Worked fine on OpenBSD

It matters not how straight the gate,
How charged with punishments the scroll,
I am the master of my fate;
I am the captain of my soul.

***William Henley***


Report •

#7
January 14, 2011 at 13:04:17
First, all the code mentioned her works on my Linux system running Ubuntu 10.

While ls *[a-z] does not run the standard ls command on my Solaris 9 box, this command does work:

/usr/xp4g/bin/ls *[a-z]

Under Solaris, xpg4 contains copies of unix commands that are considered a superset of the Posix standard. I believe that pattern matching was a relative late addition to Posix.

ijack: I never could get ls --hide=*[!a-z] to work on either of my Solaris ls commands - athough, as I stated, it did work on Ubuntu. Perhaps you are using a GNU version of the ls command on your Solaris system?



Report •

#8
January 15, 2011 at 02:58:54
@ijack Thanks , could u please explain command and regexp in detail? Can I use --hide =*[] in nested way bcoz I have one more assignment qs
i.e show file ends with small letter but not with 'a' n 'c'
If yes then how ? or any other solution

Report •

#9
January 16, 2011 at 12:43:05
nails - I'm testing on Solaris Express 11. I'm not sure what version of ls it uses, but I was surprised to find that it behaved differently to Linux and FreeBSD. The directory that you mention doesn't exist on my system.

harssha - ls just lists everything; the --hide switch filters out everything that matches the regular expression. The regular expression *[!a-z] matches everything that does not end in a lc letter (the ! negates the following part of the expression).

You can have as many --hide switches as you like. So:

ls --hide=*a --hide=*c --hide=*[!a-z]

does the job.


Report •

#10
January 17, 2011 at 19:23:14
ijack:

Solaris Express 11 is the latest Solaris version. I believe it's the first major release since Oracle bought Sun. According to the documentation, the default PATH is /usr/gnu/bin contains many GNU tools.

Read about it here, page 8:

http://www.oracle.com/technetwork/s...


Report •

#11
January 18, 2011 at 04:08:50
Thanks for that. That directory does exist and has a version of ls in it that is much bigger than that in /usr/bin. But, in this case it behaves in the same way. I can only think that it is something to do with the way that the shell is expanding *[a-z]. It does the same with sh, csh, or bash. But it works as expected if you use:

ls *[abcdefghijklmnopqrstuvwxyz]

Not very convenient!


Report •

Ask Question