Skip to content

Instantly share code, notes, and snippets.

@l0b0
l0b0 / safe-count-files.sh
Created November 26, 2010 14:49
Count the number of files in a directory
# This works even with really weird filenames like $'--$`\! *@ \a\b\e\E\f\n\r\t\v\\\"\' '
file_count()
{
if [ ! -e "$1" ]
then
exit 1
fi
local -i files=$(find "$(readlink -f -- "$1")" -type f -print0 | grep -cz -- -)
echo $files