How can I get a count of files in a directory using the command line?

Using a broad definition of "file"

ls | wc -l

(note that it doesn't count hidden files and assumes that file names don't contain newline characters).

To include hidden files (except . and ..) and avoid problems with newline characters, the canonical way is:

find . ! -name . -prune -print | grep -c /

Or recursively:

find .//. ! -name . -print | grep -c //

For narrow definition of file:

 find . -maxdepth 1 -type f | wc -l

ls -1 | wc -l

...

$ ls --help | grep -- '  -1'
    -1                         list one file per line

...

$ wc --help | grep -- '  -l'
    -l, --lines            print the newline counts

PS: Note ls -<number-one> | wc -<letter-l>

Tags:

Command Line