Pages

Friday, June 2, 2017

How to Find Number of Files in a Directory and Subdirectories

In this guide, we will cover how to display the total number of files in the current working directory or any other directory and its subdirectories on a Linux system.
We will use the find command which is used to search for files in a directory hierarchy together with wc command which prints newline, word, and byte counts for each file, alternatively data read from standard input.

Following are the options that we can use with find command as follows:
  1. -type – specifies the file type to search for, in the case above, the f means find all regular files.
  2. -print – an action to print the absolute path of a file.
  3. -l – this option prints the total number of newlines, which is equals to the total number of absolute file paths output by find command.
The general syntax of find command.
# find . -type f -print | wc -l
$ sudo find . -type f -print | wc -l
Important: Use sudo command to read all files in the specified directory including those in the subdirectories with superuser privileges, in order to avoid “Permission denied” errors as in the screen shot below:
Find Number of Files in Linux
Find Number of Files in Linux
You can see that in the first command above, not all files in the current working directory are read by find command.

      ▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
                                                    ► Read more: http://adf.ly/1n5Z40
      ▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

No comments:

Post a Comment