How to display the number of files in current and subdirectories on Linux system
This article mainly explains "how to display the number of files in the current and subdirectories on Linux system". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian slowly and deeply to study and learn together "how to display the number of files in the current and subdirectories on Linux system"!
In this guide, we'll show you how to display the number of files in the current working directory or any directory and its subdirectories on a Linux system.
We'll use the find command, which searches for files in the directory hierarchy, and the wc command, which prints newlines, words, and byte counts for each file or from standard input.
Here are the options we use in the find command, as follows:
-type -Specifies the file type to search for, in the above case f means find all regular files.
-print -Print the absolute path of the file.
Here are the options we use in the wc command, as follows:
-l -This option prints the total number of newlines, i.e. the total number of absolute file paths output by the find command.
General syntax for the find command.
# find . -type f -print | wc -l $ sudo find . -type f -print | wc -l
Important: Use sudo to read all files in the specified directory, including files in subdirectories with superuser privileges, to avoid the "Permission denied" error, as shown in the screenshot below:
Number of files in Linux
As you can see, of the *** commands above, the find command does not read all files in the current working directory.
Here are more examples showing the total number of regular files in the/var/log and/etc directories, respectively:
$ sudo find /var/log/ -type f -print | wc -l $ sudo find /etc/ -type f -print | wc -l
For more examples of the find and wc commands in Linux, check out the following series of articles for additional usage options, tips, and related commands:
35 examples of "find" commands in Linux
How to Find Recent or Today's Modified Files in Linux
Find ten directories and files occupying *** in Linux
6 useful examples of "wc" commands to count lines, words and characters
Thank you for reading, the above is "how to display the number of files in the current and subdirectories on the Linux system" content, after learning this article, I believe everyone on how to display the number of files in the current and subdirectories on the Linux system This problem has a deeper understanding, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!