Ubuntu – Unsure of grep output because of bash wildcards

bashgrep

I am trying to get a better understanding of bash commands.

ls /usr/include/ | grep \.h

It is supposed to list all the files ending in .h (C libraries) but it outputs more than expected.

thread_db .h

uchar .h

netash

If someone could also tell me how to bold the .h in the question without separating it from the rest of the name with a space, i would appriciate it.

Best Answer

In grep, your pattern is matching .h (any character followed by h) anywhere in the line, not just at the end.

You basically have two problems:

  1. Quoting/escaping issue

  2. Regex pattern issue

Explanations:

  1. You have escaped . using \. and have not used any quotes or another \, so the escaping would escape the shell, and grep would get the Regex pattern as .h, in Regex term which means any character followed by a h. Your target is to make grep treat the . literal, so you need to quote or add another \ like:

    '\.h'
    \\.h
    
  2. Now the second problem, in regex, the end of a line is expressed by the quantifier $, so as you need to match the .h at the end of the line:

    '\.h$'
    

So, together the correct usage would be:

ls /usr/include/ | grep '\.h$'

Now the most important thing, Don't parse ls.

What you are doing can be easily and efficiently done by basic shell globbing:

ls /usr/include/*.h
Related Question