You may have an occasion that you want to know the number of files in a directory. There are a couple of simple ways to do this. The first way is to list the files with ls and count them with wc. No, wc doesn't stand for the place you go to relieve yourself; in this case, it stands for word count. The command looks like this:
This command works pretty well, but has one drawback. It will count everything that ls outputs, including directories. You will get the same results from:
In this command, grep -c counts all the files that match the "." wildcard.
What if you don't want directories listed? With some additional options you can add some indicators to the directory and symbolic link listings and then filter them out with grep. The -F switch on ls will add the indicators. The -v switch on grep will be used to exclude the indicator patterns.
To count all files except directories, use this command.
To include directories, but not symbolic links, use:
To count files excluding directories and symbolic links, use:
#
Mike's reply works recursively. To make Linerd's post work recursivley, just add the -R switch: ls -AFR | grep -vc [/,@]
#
This works too
find . -type f | wc -l
#
#