How to count files in folder (Modicia Linux)

Written by Administrator on Thursday April 23, 2020

When you are working with huge amount of files, sometimes you need to count them. In Linux you don't have to install any additional software to do it. Linux terminal and bash is a very powerful tool for files routine.

Count files and directories

To get the number of non-hidden files and folders in defined folder you should run this command:

$ ls | wc -l

Example:

count files and directories in Linux - example


Count only files (not hidden) in a folder

$ ls -p | grep -v / | wc -l

Count only hidden files in a specific folder

$ ls -ldp .?* | grep -v / | wc -l

Here we are counting all files, whose names begin with a dot, but you should keep in mind, that in your directory there will be always a "dot directory", which redirects to a up level. That's why you should decrease result number of -1. 

Count files with specific (particular) extension in a folder

$ ls -lR | *.png | wc -l

Count only directories (not files) in specific folder

$ ls -lR | grep ^d | wc -l


Counting files in folder can be useful function for analyzing logs and images. Sometimes it happens that your script is producing a huge amount of files and you have to know, how many files were generated. Linux terminal is an easy and powerful tool for categorizing, counting and renaming purposes.