How to Count Number of Files within a Directory in Linux

count number files directory linux

Have you ever racked your brain for a way that you can count the number of files within a directory and sub directories in Linux?

In this brief article, we'll look at how you can do that in a simple yet accurate way.

1) Using wc command

WC command, short for Word Count, is a command line tool in Unix/Linux systems used for printing newlines, counting number lines & characters in a file. The command can also be combined with other piping operations for general counting functions.

To count the number of files in a directory, use the syntax below

# ls -1 | wc -l

Let us break down the Syntax and see what does what

ls : Lists the files
-1  : This is a ONE. It prints one entry per line. To print hidden files, change it to -1a
|    : Pipes output into...
wc  : Wordcount
-l  : Counts the lines

To count the number of files outside the directory, you can specify the directory name as shown

# ls directory_name | wc -l

To demonstrate how this commands works, let's create a new directory test_folder, and navigate into it.

# mkdir test_folder && cd test_folder

Next, we are going to create a few text files for  demonstration purposes

# touch  file1.txt file2.txt file3.txt file4.txt file5.txt

Let us confirm the existence of the files by listing them  using the ls command

# ls -l


Count Number of Files Within a Directory

Great, now let's  count the files

# ls -1 | wc -l


Count Number of Files Within a Directory

Similarly, you can exit the directory using cd command

# cd ..

And run

# ls test_folder | wc -l


Count Number of Files Within a Directory

2) Using tree command

tree  is a Unix/Linux command line tool that recursively prints directories in a tree-like format. It displays each directory along with any subdirectories within it. In addition, it can also display and print the number of files in a directory.

Navigate into our sample test_folder

cd test_folder

Then, run the tree command



As you can see, we the files names alongside the count that appears at the bottom is listed. Clearly, through observation, the count matches exactly the number of files created earlier on.

4) Rsync Command

We can use rsync command to find the number of files of files, directories, symlinks.

# rsync --stats --dry-run -ax /etc /test

--dry-run is used not actually transfer the files. /test should be any empty or non existing folder.

# rsync --stats --dry-run -ax /etc /test

Number of files: 1,406 (reg: 622, dir: 187, link: 597)
Number of created files: 1,406 (reg: 622, dir: 187, link: 597)
Number of deleted files: 0
Number of regular files transferred: 622
Total file size: 2,341,951 bytes
Total transferred file size: 2,320,656 bytes
Literal data: 0 bytes
Matched data: 0 bytes
File list size: 0
File list generation time: 0.001 seconds
File list transfer time: 0.000 seconds
Total bytes sent: 64,184
Total bytes received: 4,429

sent 64,184 bytes  received 4,429 bytes  137,226.00 bytes/sec
total size is 2,341,951  speedup is 34.13 (DRY RUN)

3) Count files recursively through directories and Subdirectories

To recursively count files through directories and subdirectories using the command below

# find DIR_NAME -type f | wc -l


DIR_NAME is the directory name

- type f specifies files only

wc (Word Count) counts newlines, words, and bytes on its input

-l  Counts new lines

If you are counting files in the current directory replace the DIR_NAME with a period as shown

# find . -type f | wc -l

To include other subfolders and files within subfolders, leave out the - type f flag.

find . | wc -l

I hope that this article has been helpful and that now you can comfortably count the number of files within directories and subdirectories. Also, try ncdu tool which is a GUI tool which also helps to find the file count.

Jamie Arthur 12:05 am

About Jamie Arthur

James is a passionate Linux and Windows Systems Administrator with 4 years of experience in Linux, databases and Front-End development. He loves doing research on different Linux distributions and experimenting with installation and configuration of different services and features. His hobbies include swimming, reading novels and playing video games.

Author's All Posts
Like to become part of Linoxide Team and contribute tips? Contact us here.


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

All comments are subject to moderation.