Use the find command to check the number of files in the directory

In this guide, we will describe how to  display the number of files in the current working directory, or any directory and its subdirectories, on a Linux  system, and the wc  command , which prints each file or newline, word, and word from standard input. section count.

We will use the find  command , which is used to search for files in a directory hierarchy, the following are the options that we use in the find command as follows −

-type - Specifies the type of file to search for, in the above case f means find all regular files.

-print - print the absolute path of the file.

Following are the options used in our wc command as follows:

-l - This option prints the total number of newlines, which is the total number of absolute file paths output by the find command.

General Syntax of the find Command

# find . -type f -print | wc -l

$ sudo find . -type f -print | wc -l

PS: Use the sudo command to read all files in the specified directory, including files in subdirectories with superuser privileges, to avoid "Permission denied" errors, as shown in the screenshot below:


Find Number of Files in Linux

Number of files in Linux

You can see that in the first command above, the find command did not read all the files in the current working directory.

Here are some 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

This article was originally compiled by LCTT, and honored by Linux China

 

Guess you like

Origin blog.csdn.net/yaxuan88521/article/details/130528872