Linux: Find Number of Files in a Directory and Subdirectories

Filed Under: UNIX/Linux
Linux Find The Number Of Files In A Directory

Sometimes we want to find the number of files in a directory in Linux. For example, finding how many images are present in JournalDev WordPress uploads directory.

There are various ways to do that, let’s look into some of the common scenarios and the best command to find the number of files in a directory.

1. Find the Number of Files in a Directory

We can use the ls command along with the wc command to count the number of files in a directory. Let’s count the number of files in my theme root directory.

# ls
404.php    archive.php   functions.php  inc        layouts            phpcs.xml.dist     screenshot.png    single.php      webpack
LICENSE    comments.php  header.php     index.php  package-lock.json  postcss.config.js  search.php        src             woocommerce.css
Plugins    dist          home.php       js         package.json       readme.txt         sidebar-left.php  style.css
README.md  footer.php    images         languages  page.php           rtl.css            sidebar.php       template-parts
# ls -1 | wc -l
34
Linux Find Number Of Files In a Directory
Linux Find Number Of Files in a Directory

If you look at the image, the blue coloured items are directories. They are also included as file in the output.

What if we want to count only files and not directories?

# ls -p | grep -v / | wc -l
24
#
  1. The “ls -p” command prints directory names with “/” at the end of it.
  2. The “grep -v /” command filters the output and prints only the name that doesn’t contain “/”, hence leaving out all the directories.
  3. Finally, “wc -l” count the lines in the output and prints it.

Similarly, if you want to find the number of directories only inside a directory, use the below command.

# ls -p | grep / | wc -l
10
#

Note: The above commands don’t look for the hidden files, if you want the count to include hidden files too, then use “-a” option with the ls command.

# ls -a1 | wc -l
41
#

This command will count “.” and “..” too, so you will have to take account of that in your shell script if needed.

2. Find Number of Files in a Directory and Subdirectories Recursively

The above examples are good to count files and directories in a directory. But, if you want to count the number of files including subdirectories also, you will have to use the find command.

# find . -type f
./.test_file
./functions.php
./logger/class-logger-writter.php
./logger/class-logger-export.php
./logger/assets/js/base.js
./logger/assets/css/base.css
./logger/assets/css/base.less
./logger/assets/css/base.css.map
./logger/class-logger-reader.php
./class-protector.php
./class-anti-spam-plugin.php
# find . -type f | wc -l
11
#
  1. The find command “-type f” option is used to look for regular files.
  2. This command will ignore all the directories, “.”, and “..” files. But, it will include hidden files in the output.
  3. The “wc -l” command will count the total number of lines and print it, thus giving us the count of files.
Linux Find Number Of Files In A Directory Recursively
Linux Find Number Of Files In A Directory Recursively

References:

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages