With zsh
:
set -o extendedglob # best in ~/.zshrcc_regular_files=( ${(0)^"$(locate -0 "${${PWD%/}//(#m)[]\\*?]/\\$MATCH}/*.c")"}(N.))echo there are at least $#c_regular_files regular files whose name ends in .c
- We need to escape the
[
,?
,\
and*
in$PWD
forlocate
not to interpret them as wildcard operators (it would be even worse with-r
for regexps which have more operators including.
, common in file names) $PWD
==/
has to be treated specially; with$PWD
instead of${PWD%/}
, we would be runninglocate -0 "//*.c"
which wouldn't return anything.-0
for files to be NUL-delimited (newline wouldn't work as it's allowed in file paths)..
is for regular file. Contrary to[ -f
, that excludes symlinks to regular file. If you want every non-symlink.c
file (allowing any other type of file like directory, fifo, socket...), replace.
with^@
.
In any case note that locate
returns a list based on the last time the locate
database was updated which may not reflect the current reality.