count the number of files in a directory using WC

To count the number of files in a directory, I typically use

ls directory | wc -l


I found this:

find DIR_NAME -type f | wc -l


  • -type f to include only files.
  • | redirects find command’s standard output to wc command’s standard input.
  • wc (short for word count) counts newlines, words and bytes on its input (docs).
  • -l to count just newlines.


  • Replace DIR_NAME with . to execute the command in the current folder.
  • You can also remove the -type f to include directories (and symlinks) in the count.
  • It’s possible this command will overcount if filenames can contain newline characters.

If you want a breakdown of how many files are in each dir under your current dir:

for i in $(find . -maxdepth 1 -type d) ; do 
    echo -n $i": " ; 
    ( find $i -type f | wc -l ) ; 

That can go all on one line, of course. The parenthesis clarify whose output wc -l is supposed to be watching (find $i -type f in this case).


For the current directory:

find . -type f | wc -l

If you want to know how many files and sub-directories exist from the present working directory you can use this one-liner

find . -maxdepth 1 -type d -print0 | xargs -0 -I {} sh -c ‘echo -e $(find {} | wc -l) {}’ | sort -n
This will work in GNU flavour, and just omit the -e from the echo command for BSD linux (e.g. OSX).



I would like to give a different approach with filtering for format. Example counts all available grub kernel modules:

ls -l /boot/grub/*.mod | wc -l




ls -l | grep -e -x -e -dr | wc -l

1.long list 2.filter files and dirs 3.count the filtered line no



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

I did this by:

du -a | cut -d/ -f2 | sort | uniq -c | sort -nr

which will return the number of files in the directory + 1 (the directory itself).