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:

  • -type – specifies the file type to search for, in the case above, the f means to find all regular files.
  • -print – an action to print the absolute path of a file.
  • -l – this option prints the total number of new lines, which equals to the total number of absolute file paths output by find command.

 

The general syntax of the find command.

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

 

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

 

The following are extra examples to show the total number of regular files in /var/log and /etc directories respectively:

$ sudo find /var/log/ -type f -print | wc -l
$ sudo find /etc/ -type f -print | wc -l

 

Was this answer helpful? 0 Users Found This Useful (0 Votes)