Count Number of Files in a Directory in Linux
Here are several ways to count the number of files in a directory in Linux command line.
Table of Contents
I presume you are aware of the wc command for counting number of lines. We can use the same wc command with ls command to count the number of files in a directory.
This task seems simple but could soon turn slightly complex based on your need and definition of counting files. Before I confuse you further, let’s see about various use cases of counting the number of files in Linux.
Count number of files in directory in Linux
Let me first show you the content of the test directory I am going to use in this tutorial:
You can see that it has 9 files (including one hidden file) and 2 sub-directories in that directory. But you don’t have to do it manually. Let’s count the number of files using Linux commands.
Count number of files and directories (without hidden files)
You can simply run the combination of the ls and wc command and it will display the number of files:
This is the output:
There is a problem with this command. It counts all the files and directories in the current directories. But it doesn’t see the hidden files (the files that have name starting with a dot).
This is the reason why the above command showed me a count of 10 files instead of 11 (9 files and 2 directories).
Count number of files and directories including hidden files
You probably already know that -a option of ls command shows the hidden files. But if you use the ls -a command, it also displays the . (present directory) and .. (parent directory). This is why you need to use -A option that displays the hidden files excluding . and .. directories.
This will give you the correct count of files and directories in the current directory. Have a look at the output that shows a count of 11 (9 files and 2 directories):
You can also use this command to achieve the same result:
Note that it the option used is 1 (one) not l (L). Using the l (L) option displays an additional line at the beginning of the output (see ‘total 64’ in the directory output at the beginning of the article). Using 1 (one) lists one content per line excluding the additional line. This gives a more accurate result.
Count number of files and directories including the subdirectories
What you have see so far is the count of files and directories in the current directory only. It doesn’t take into account the files in the subdirectories.
If you want to count the number of files and directories in all the subdirectories, you can use the tree command.
This command shows the directory structure and then displays the summary at the bottom of the output.
As you can see in the output, it shows that there are 7 directories and 20 files in total. The good thing about this result is that it doesn’t count directories in the count of files.
Count only the files, not directories
So far, all the solutions we have seen for counting the number of files, also take directories into account. Directories are essentially files but what if you want to count only the number of files, not directories? You can use the wonderful find command.
You can run this command:
The above command searched for all the files (type f) in current directory and its subdirectories.
Count only the files, not directories and only in current directory, not subdirectories
That’s cool! But what if you want to count the number of files in the current directory only excluding the files in the subdirectories? You can use the same command as above but with a slight difference.
All you have to do is to add the ‘depth’ of your find. If you set it at 1, it won’t enter the subdirectories.
Here’s the output now:
In the end…
In Linux, you can have multiple ways to achieve the same goal. I am pretty sure there can be several other methods to count the number of files in Linux. If you use some other command, why not share it with us?
I hope this Linux tutorial helped you learn a few things. Stay in touch for more Linux tips.
Recursively counting files in a Linux directory
How can I recursively count files in a Linux directory?
But when I run this it returns the following error.
find: paths must precede expression: ¦
21 Answers 21
Explanation:
- -type f to include only files.
- | (and not ¦ ) redirects find command’s standard output to wc command’s standard input.
- wc (short for word count) counts newlines, words and bytes on its input (docs).
- -l to count just newlines.
Notes:
- Replace DIR_NAME with . to execute the command in the current folder.
- You can also remove the -type f to include directories (and symlinks) in the count.
- It’s possible this command will overcount if filenames can contain newline characters.
Explanation of why your example does not work:
In the command you showed, you do not use the «Pipe» ( | ) to kind-of connect two commands, but the broken bar ( ¦ ) which the shell does not recognize as a command or something similar. That’s why you get that error message.
For the current directory:
If you want a breakdown of how many files are in each dir under your current dir:
That can go all on one line, of course. The parenthesis clarify whose output wc -l is supposed to be watching ( find $i -type f in this case).
after installing the tree package with
(on a Debian / Mint / Ubuntu Linux machine).
The command shows not only the count of the files, but also the count of the directories, separately. The option -L can be used to specify the maximum display level (which, by default, is the maximum depth of the directory tree).
Hidden files can be included too by supplying the -a option .
On my computer, rsync is a little bit faster than find | wc -l in the accepted answer:
The second line has the number of files, 150,481 in the above example. As a bonus you get the total size as well (in bytes).
- the first line is a count of files, directories, symlinks, etc all together, that’s why it is bigger than the second line.
- the —dry-run (or -n for short) option is important to not actually transfer the files!
- I used the -x option to «don’t cross filesystem boundaries», which means if you execute it for / and you have external hard disks attached, it will only count the files on the root partition.
-type f | wc -l took 1.7/0.5/1.33 seconds (real/user/sys). rsync —stats —dry-run -ax
/xxx took 4.4/3.1/2.1 seconds. That’s for about 500,000 files on SSD. – slim Jul 7 ’17 at 9:58
Since filenames in UNIX may contain newlines (yes, newlines), wc -l might count too many files. I would print a dot for every file and then count the dots:
Combining several of the answers here together, the most useful solution seems to be:
It can handle odd things like file names that include spaces parenthesis and even new lines. It also sorts the output by the number of files.
You can increase the number after -maxdepth to get sub directories counted too. Keep in mind that this can potentially take a long time, particularly if you have a highly nested directory structure in combination with a high -maxdepth number.
If you want to know how many files and sub-directories exist from the present working directory you can use this one-liner
This will work in GNU flavour, and just omit the -e from the echo command for BSD linux (e.g. OSX).
If you want to avoid error cases, don’t allow wc -l to see files with newlines (which it will count as 2+ files)
e.g. Consider a case where we have a single file with a single EOL character in it
Since at least gnu wc does not appear to have an option to read/count a null terminated list (except from a file), the easiest solution would just be to not pass it filenames, but a static output each time a file is found, e.g. in the same directory as above
Or if your find supports it
You can use the command ncdu . It will recursively count how many files a Linux directory contains. Here is an example of output:
It has a progress bar, which is convenient if you have many files:
To install it on Ubuntu:
Benchmark: I used https://archive.org/details/cv_corpus_v1.tar (380390 files, 11 GB) as the folder where one has to count the number of files.
- find . -type f | wc -l : around 1m20s to complete
- ncdu : around 1m20s to complete
2 million files, so the speed is not too bad. – Franck Dernoncourt Apr 24 ’18 at 19:34
If what you need is to count a specific file type recursively, you can do:
-l is just to display the number of lines in the output.
If you need to exclude certain folders, use -not -path
To determine how many files there are in the current directory, put in ls -1 | wc -l . This uses wc to do a count of the number of lines (-l) in the output of ls -1 . It doesn’t count dotfiles. Please note that ls -l (that’s an «L» rather than a «1» as in the previous examples) which I used in previous versions of this HOWTO will actually give you a file count one greater than the actual count. Thanks to Kam Nejad for this point.
If you want to count only files and NOT include symbolic links (just an example of what else you could do), you could use ls -l | grep -v ^l | wc -l (that’s an «L» not a «1» this time, we want a «long» listing here). grep checks for any line beginning with «l» (indicating a link), and discards that line (-v).
Relative speed: «ls -1 /usr/bin/ | wc -l» takes about 1.03 seconds on an unloaded 486SX25 (/usr/bin/ on this machine has 355 files). » ls -l /usr/bin/ | grep -v ^l | wc -l » takes about 1.19 seconds.
Create an array of entries with ( ) and get the count with #.
Ok that doesn’t recursively count files but I wanted to show the simple option first. A common use case might be for creating rollover backups of a file. This will create logfile.1, logfile.2, logfile.3 etc.
Recursive count with bash 4+ globstar enabled (as mentioned by @tripleee)
To get the count of files recursively we can still use find in the same way.
I have written ffcnt to speed up recursive file counting under specific circumstances: rotational disks and filesystems that support extent mapping.
It can be an order of magnitude faster than ls or find based approaches, but YMMV.
For directories with spaces in the name . (based on various answers above) — recursively print directory name with number of files within:
Example (formatted for readability):
The directory structure is better visualized using tree :
Applcation: I want to find the max number of files among several hundred directories (all depth = 1) [output below again formatted for readability]:
sort -V is a natural sort. . So, my max number of files in any of those (Claws Mail) directories is 4375 files. If I left-pad (https://stackoverflow.com/a/55409116/1904943) those filenames — they are all named numerically, starting with 1, in each directory — and pad to 5 total digits, I should be ok.
Addendum
Find the total number of files, subdirectories in a directory.