
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 tutorial, 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
Output

Great, now let's count the files
# ls -1 | wc -l
Output

Similarly, you can exit the directory using cd command
# cd ..
And run
# ls test_folder | wc -l
Output

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
tree
Output

As you can see, the names of the files alongside the count that appears at the bottom 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, directories, symlinks.
# rsync --stats --dry-run -ax /etc /test
--dry-run
is used not actually transfer the files. /test
should be an 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
Where:
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 that also helps to find the file count.
Thanks! I piped `wc` with `tree` and works pretty well for my needs of counting files.
ls -1 | wc -l will give 6 not 5
i've found another way to count files and directorys :
for files : ll | cut -c1 | grep - | wc -l
for directorys : ll | cut -c1 | grep d | wc -l
Hey Hamza,
Excellent. Thanks for the tips. Hope this helps other readers.