1

I need to list files and dirs with names starting with jre (small and capital letters are allowed).

When I execute:

ls | grep jre

...it greps just low capital letters and gives output with jre not only in start position. Actually I have feeling that grep is not a good choice at all in this case.

How to solve my problem?

vico
  • 4,717
  • 23
  • 66
  • 89

2 Answers2

1

you can use find. In your case you can run

find -maxdepth 1 -iname 'jre*'

Explanation:

find searches for files/directories with the given parameters: -maxdepth 1 restricts the search to the current directory. Otherwise, it would search in all subdirectories too. -iname takes the pattern and searches case insensitive. The pattern itself 'jre*' means the string should begin with "jre" and the continue with a arbitrary number of cahracters.

Alternatively, if you want to use grep:

ls | grep -i '^jre'

-i toggles case-insensitive search and the ^ results in finding only matches with "jre" at the beginning

Wayne_Yux
  • 4,942
0

Find should do the trick:

find . -iname 'jre*'
Jakob Lenfers
  • 1,105
  • 7
  • 17