Count files and folders linux

Count Number of Files in a Directory in Linux

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).

Читайте также:  Windows system files checksums

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.

Читайте также:  Анализ файлов dmp windows

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.

Источник

Оцените статью