Sometimes you might want to find out the number of files in a given directory. This tutorial covers how to count files in a one directory, not subdirectories. If you want to count files recursively, check this article: Count Files in Directory Recursively in Linux
To accomplish this task, we are going to use
ls and pipe it to
wc -l which count lines of the input. You should also decide whether to include or exclude hidden files.
Our directory structure in this example is (including hidden files and one subfolder):
$ ls -la total 60 drwxr-xr-x 3 webadmin apache 4096 Dec 28 16:30 . drwxr-xr-x 5 webadmin apache 4096 Dec 28 14:54 .. -rwxr-xr-x 1 webadmin apache 29615 Dec 16 14:57 booking.php -rw-r--r-- 1 webadmin apache 15 Dec 28 14:54 file1.php -rw-r--r-- 1 webadmin apache 20 Dec 18 14:57 file3.php -rw-r--r-- 1 webadmin apache 9 Dec 28 16:30 .gitignore -rw-r-xr-- 1 webadmin apache 22 Jun 14 2019 README.md drwxr-xr-x 2 webadmin apache 4096 Dec 28 16:30 subfolder
$ ls | wc -l 5
Note! Also the
subfolder is included in the number.
$ ls -A | wc -l 6
subfolder is included in the number as well, and also the hidden file
.. are not included because we used the
-A option, not
-a which would list also them.