How To Count Files In Directory On Linux?

Linux system administrator may require to get a count of the files for a specific path which can be the current working directory or another path. There are different ways to count files of a directory where the files can be counted recursively or for the only specified directory.

Count Files with find Command

The find command is used to sing different files and folders with different search options. The find command can be also used to count files for the specified directory. The wc command will be also used to count listed files for the specified directory. In the following example, we count files for the current working directory.

$ find . -type f | wc -l

Alternatively, we can specify another path in order to count files. In the following example, we count files in the /etc/.

$ find /etc -type f | wc -l

Count Files Recursively with find Command

The find command can be also used to count files recursively. In previous examples, we have used the find command to count only for the specified path. But child directories may contain files and we can count these files with the find command recursively. In the following example, we will count files recursively for the current working directory.

$ find . -r -type f | wc -l

Alternatively, we can count files for a specified path with all its child directories by using find command recursively.

$ find /etc -r -type f | wc -l

Count Files with ls Command

The ls command is used to list files and directories. But we can also use the ls command in order to count files for the current working directory or for a specific path. For the current working directory, the file count can be listed below.

$ ls -l | wc -l

Alternatively, we can specify a different path than the current working directory where the specified path files are counted.

$ ls -l /etc | wc -l

Count Files with tree Command

The tree command is used to list current files in a visual way with subdirectories. By running the tree command current directory files can be counted like below.

$ tree 
Count Files with tree Command

Leave a Comment