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?

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

| improve this answer | |
0

Find should do the trick:

find . -iname 'jre*'
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.