Rather than parsing the output of locate
(which is fragile and may miss things that have changed since its database was last updated, or that are not available to all users), use find
.
The following will find all .c
files in the current directory that are regular files (not symbolic links):
find . -type f -name '*.c'
Given the directory structure
.|-- file-a.c|-- file-b.c|-- file-c.c|-- file-d.c|-- link-b.c -> file-b.c`-- link-d.c -> file-d.c
This would return
./file-a.c./file-b.c./file-c.c./file-d.c
To count them:
find . -type f -name '*.c' | wc -l
or, if you have filenames with newlines in their names,
find .//. -name '*.c' -type f | grep -c //
Doing the same for symbolic links would involve changing the -type f
to -type l
.