regex in ls

Hi Experts,

I am using ls with regex in the below manner:

VAR="*.txt *.TXT"

ls -l $VAR

This is working fine if I have both txt and TXT extension files in my directory. But if any of them is not present, its throwing errors, that .TXT file not found in the directory. So what am i missing ?
main requirement is to search for these file extensions(
.txt or *.TXT or both).. if not found then exit.

Thanks alot guys.

1) It's not a regex, it's a glob.
2) The shell does it. ls is useless here.

It's true. Try it:

VAR="*.txt *.TXT"
echo $VAR

Do you need to find .txt OR .txt? Or do you need to find both?

1 Like

hmm.. i didn't understand :(.
I want to list the files in the directory with extensions *.txt or *.TXT or both.
In the nutshell, if i have three files a.txt b.TXT c.dat,

ls should list a.txt and b.TXT.. and if it doesn't find the files of this extensions.. then exit.

Many Thanks.

What don't you understand? Ask more specific questions and I can give more specific answers.

If you really want ls, you can just ignore ls' error messages.

ls $VAR 2>/dev/null

okay.. thanks alot.. i just meant that the below statements are not clear.
1) It's not a regex, it's a glob.
2) The shell does it. ls is useless here.

Anyways.. i got my answer. Many thanks.

1 means what it says. It's not a regex. Regexes are a lot more complicated. A regex would be like

\.[tT][xX][tT]$

.

2 also means what it says. The glob is not fed into ls. The shell finds all the files for you before it's even run. Try running the command I gave you -- it will show you what it does.

Okay i got it now. Thanks alot.