Count number of files within a directory in Linux?

LinuxLs

Linux Problem Overview


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

ls directory | wc -l

But is there another command that doesn't use wc ?

Linux Solutions


Solution 1 - Linux

this is one:

ls -l . | egrep -c '^-'

Note:

ls -1 | wc -l

Which means: ls: list files in dir

-1: (that's a ONE) only one entry per line. Change it to -1a if you want hidden files too

|: pipe output onto...

wc: "wordcount"

-l: count lines.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKanturaView Question on Stackoverflow
Solution 1 - LinuxSajad KaruthedathView Answer on Stackoverflow