• Rob's side project: I recently started Gun-Forums.com and we currently have a $100 raffle - if you're into guns, join up and enter the raffle to win $100.00. Gun Forums

Grep with number of caractere


New Member
How to grep by number of caractere
for exp : if i wanna grep for file with 5 caractere like momos
how can i do that ?
ls | grep -E [a-z]\{5\}
kind regards


Well-Known Member
I think you've almost answered your own question there.
NOTE: egrep is the same as grep -E

What you have there:
ls | egrep [a-z]{5}

That will match any file that has a 5 letter name with all lowercase characters. But it will also match any files that have more than 5 lowercase characters.

If you only want matches that exactly match the expression, then you can either surround the expression with ^ and $
ls | egrep ^[a-z]{5}$

The above will match all files that contain exactly 5 lowercase letters.

Another way of doing exactly the same thing is to use greps -x option.
ls | egrep -x [a-z]{5}

Above will match all files that contain exactly 5 lowercase letters.
Either of those will do what you want.

But if you want to include any valid uppercase, or lowercase alphabetic, or numeric digits, you could use a character class.
ls | egrep -x [[:alnum:]]{5}

The above matches all files that contain exactly 5 characters (uppercase, lowercase, or numeric characters for the environments current LOCALE setting)

Another way to do it would be:
ls | egrep -x .{5}
Which would match all 5 letter file-names, regardless of which characters were used.
Last edited:

Members online

Latest posts