Quantcast
Channel: Count number of files that are not symbolic links in output from "locate" - Unix & Linux Stack Exchange
Viewing all articles
Browse latest Browse all 5

Answer by Stéphane Chazelas for Count number of files that are not symbolic links in output from "locate"

$
0
0

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 for locate 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 running locate -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.


Viewing all articles
Browse latest Browse all 5

Trending Articles