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
- https://devconnected.com/how-to-count-files-in-directory-on-linux/
- https://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x700.html
- https://www.theurbanpenguin.com/counting-files-directories-linux/
Read More
- [Linux] Use “free” command to Release Memory
- [Linux] Use “ssh” Command to log in the Remote Server and Execute Commands
- [Linux] Use “ln” Command to Create Soft Link and Hard Link Files
- [Linux] Use mv and rename command to rename a file
- [Linux] Use “ls” command to display different colors and orders according to different file extensions