Skip to content

Instantly share code, notes, and snippets.

@TheRakeshPurohit
Forked from l0b0/safe-count-files.sh
Created August 29, 2023 08:15
Show Gist options
  • Save TheRakeshPurohit/d499b12a91f18a4dfcf9e7c51fd6e320 to your computer and use it in GitHub Desktop.
Save TheRakeshPurohit/d499b12a91f18a4dfcf9e7c51fd6e320 to your computer and use it in GitHub Desktop.
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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment