Operating System - Linux
1752800 Members
5592 Online
108789 Solutions
New Discussion юеВ

How to list the files in a directory ignoring the cases of filenames

 
SOLVED
Go to solution
CA1490051
Frequent Advisor

How to list the files in a directory ignoring the cases of filenames

Hi All,

I want to list down all the files ina directory but by ignoring the case (Capital or Small letters) of the filename.

Ex - if 2 files are there
Makefile
makefile
Make2

i want to give some thing like
ls | grep -i "M*"
to list down all the files starting with "M".

Can any one Please help me to acieve the same.

thanks and advance
Vikram
10 REPLIES 10
Steven E. Protter
Exalted Contributor

Re: How to list the files in a directory ignoring the cases of filenames

Shalom Vikram,

find . -type f

Ignores directories and such.

man find

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Sandman!
Honored Contributor
Solution

Re: How to list the files in a directory ignoring the cases of filenames

I am not sure I understand your question but let me try. It would help immensely if you could provide a concrete sample of the input and the desired output.

# ls [mM]*

~hope it helps
Dennis Handly
Acclaimed Contributor

Re: How to list the files in a directory ignoring the cases of filenames

>I want to list all the files in a directory but by ignoring the case of the filename.
>Ex - if 2 files are there: Makefile makefile Make2
>i want to give something like: ls | grep -i "M*"
>to list down all the files starting with "M".

To list all files starting with "M" you can use:
$ ls [Mm]*

You could use grep too:
$ ls | grep -i "^M"
Sanjiv Sharma_1
Honored Contributor

Re: How to list the files in a directory ignoring the cases of filenames

I think the following will meet your requirement.

$ ls [mM]*
Everything is possible
CA1490051
Frequent Advisor

Re: How to list the files in a directory ignoring the cases of filenames

Hi All,

Thnk you very much for the answers
ls | grep '[mM]*\.txt'
is working for me

but one problem is if i have files as below
ABCDxxxxMakefile.txt
ABCDyyymakefile.txt

if i give
ls | grep '^ABCD*[mM]*\.txt'

it is not listing the above files.

Please correct the errors in my regular expressions.
Basically the * (Ex- ls | grep '*[mM]*')at the beginning of the string is not working.


thanks and regards
Vikram

James R. Ferguson
Acclaimed Contributor

Re: How to list the files in a directory ignoring the cases of filenames

Hi Vikram:

You need:

# ls | grep '^ABCD.*[mM]*\.txt'

...to list the files. The dot (".") specifies any character and the asterisk ("*") following says zero or more of the any character.

Regards!

...JRF...
spex
Honored Contributor

Re: How to list the files in a directory ignoring the cases of filenames

$ ls | grep '^ABCD.*[mM].*\.txt'
Peter Nikitka
Honored Contributor

Re: How to list the files in a directory ignoring the cases of filenames

Hi,

what about
ls -d *[mM]*

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Dennis Handly
Acclaimed Contributor

Re: How to list the files in a directory ignoring the cases of filenames

>Please correct the errors in my regular expressions.
>Basically the * (Ex- ls | grep '*[mM]*') at the beginning of the string is not working.

As JRF and spex said, you need to use ".*" for "*", "." for "?". You also may need to anchor your RE at the beginning with "^" or end with "$".
Note: you don't need the ".*" at the end or beginning of your RE if it isn't anchored.

The former is a regular expression. The latter is a Pattern Matching Notation.
See regexp(5).

If our answers are helpful, please read and assign points:
http://forums1.itrc.hp.com/service/forums/helptips.do?#33

And if finished, close the thread.