Skip to content

[Linux] Calculate How Many Files In a Folder

In linux system, how can we view the number of the files in a folder? It is a common request, so I record it today.

Record the solved problems at hand, so that you can have an impression when you encounter problems in the future, and you can come to your website to find it.


ls command and wc command

In fact, these two instructions are probably what we need most.

ls command shouldn't need to be said much, it can display the files under the directory; wc command is also quite well-known, can be used to calculate the number of display lines of the output terminal.

For example:

echo -e "test\ntest"

Output:

test
test


This way we will output two lines of "test". Then if we match the "wc" command at this time:

echo -e "test\ntest" | wc -l

Output:

2

We will output the result of "2". So everyone must have figured it out. In fact, we only need to use the "ls" command to calculate the number of files.

Suppose I have a folder called "test" with 256 files underneath

ls -l test/ | wc -l

By the way, if you need more accurate statistics, you can use "grep" to limit the search conditions.

It can only be said that counting numbers in Linux is really convenient.


References


Read More

Tags:

Leave a Reply