man
man grep
tells us that
The period . matches any single character.
This means that .c
matches any single character followed by a c. Since you haven't anchored your expression, this matches any string that contains any character followed by a c.
Let's test that. What if we don't use the -v
flag?
test
Let's see. First, we set up a test
~ $ mkdir test
~ $ cd test
~/test $ touch arrays.c
~/test $ touch basic0
~/test $ touch basic0.c
~/test $ touch fromfb
~/test $ touch fromfb.c
~/test $ touch oute
~/test $ touch oute.c
~/test $ touch segmen.c
~/test $ touch simp
~/test $ touch simp.c
~/test $ ls
arrays.c basic0.c fromfb.c oute.c simp
basic0 fromfb oute segmen.c simp.c
Then we test ls | grep .c
:
~/test $ ls|grep .c
arrays.c
basic0
basic0.c
fromfb.c
oute.c
segmen.c
simp.c
As you can see, not only do the intended C sources match, but the binary file basic0
matches as well. The i is matched by the .
and the c is matched by (no surprise here) the c
.
solution
You want to match C sources to exclude them. These files end with .c, so we tell grep to match those.
~/test $ ls|grep "\.c$"
arrays.c
basic0.c
fromfb.c
oute.c
segmen.c
simp.c
We've done two things here. First, we've escaped the dot, to make it a literal dot instead of "any single character". But this would still match a file called foo.c.bar
which may or may not be your goal.
Assuming it's not, we've also anchored the c to be the last character. Now you're matching the strings that end with a dot and a c.
Reapplying -v
is left as an exercise for the reader.
ls | grep -v "\.c"
("." escapes dot sign) – bob_saginowski Nov 10 '16 at 12:23ls | grep
is almost always not what you want. You probably wantfind
. – HalosGhost Nov 10 '16 at 14:12