Skip to content

Instantly share code, notes, and snippets.

@alexzhan
Created November 15, 2010 05:37
Show Gist options
  • Save alexzhan/700080 to your computer and use it in GitHub Desktop.
Save alexzhan/700080 to your computer and use it in GitHub Desktop.
Number of files in directory
# the number of files and directories in a directory
ls | wc -l
# A directory is a file but its not a regular file, so the question is not clear enough
# If he does not want directory, then something awfull like this should do:
echo $(($(ls -l | grep -v ^d | wc -l)-1))
ls *.jpg | wc
ls *.jpg | wc -l
ls -l | grep ^- | wc
ls -l | grep ^- | wc -l
ls -l | grep ^d | wc -l
ls -l | grep ^l | wc -l
# Actually that's true, the grep doesn't work for regular files that have certain properties (suid,...) so here's a
# better one:
for t in files links directories; do echo `find . -type ${t:0:1} | wc -l` $t; done 2> /dev/null
# If you don't want to recurse directories (only count files in the current dir):
for t in files links directories; do echo `find . -maxdepth 1 -type ${t:0:1} | wc -l` $t; done 2> /dev/null
#Result:
655 files
6 links
213 directories
#And directory count begins at 1 because . is counted as a directory.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment