If you want to count the number of files and directories in all the subdirectories, you can use the tree command. It doesn’t take into account the files in the subdirectories. What you have see so far is the count of files and directories in the current directory only. Count number of files and directories including the subdirectories Using 1 (one) lists one content per line excluding the additional line. 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). Note that it the option used is 1 (one) not l (L). You can also use this command to achieve the same result: ls -1A | wc -l Have a look at the output that shows a count of 11 (9 files and 2 directories): :~/tutorials$ ls -A | wc -l This will give you the correct count of files and directories in the current directory. This is why you need to use -A option that displays the hidden files excluding. But if you use the ls -a command, it also displays the. You probably already know that -a option of ls command shows the hidden files. Count number of files and directories including hidden files This is the reason why the above command showed me a count of 10 files instead of 11 (9 files and 2 directories). But it doesn’t see the hidden files (the files that have name starting with a dot). It counts all the files and directories in the current directories. This is the output: :~/tutorials$ ls | wc -l You can simply run the combination of the ls and wc command and it will display the number of files: ls | wc -l Count number of files and directories (without hidden files) Let’s count the number of files using Linux commands. You can see that it has 9 files (including one hidden file) and 2 sub-directories in that directory. rwxrw-r– 1 abhishek abhishek 72 Jan 21 15:44 sleep.shĭrwxr-xr-x 3 abhishek abhishek 4096 Jan 4 20:10 target rw-r–r– 1 abhishek abhishek 356 Dec 11 21:35 sherlock.txt rw-r–r– 1 abhishek abhishek 19183 Mar 18 18:46 services rw-r–r– 1 abhishek abhishek 163 Apr 13 15:07 prog.py rw-r–r– 1 abhishek abhishek 1778 Apr 29 16:16 my_zip_folder.zipĭrwxr-xr-x 4 abhishek abhishek 4096 Apr 19 19:07 newdir rw-rw-r– 1 abhishek abhishek 95 Feb 11 13:12 cpluplus.cpp rwxr–r– 1 abhishek abhishek 140 Mar 22 16:41 bash_script.sh rw-r–r– 1 abhishek abhishek 0 Apr 16 19:53. rwxr–r– 1 abhishek abhishek 456 Mar 6 16:21 agatha.txt Let me first show you the content of the test directory I am going to use in this tutorial: :~/tutorials$ ls -laĭrwxr-xr-x 4 abhishek abhishek 4096 Apr 29 17:53. Count number of files in directory in Linux Count Files Linux Before I confuse you further, let’s see about various use cases of counting the number of files in Linux. This task seems simple but could soon turn slightly complex based on your need and definition of counting files. We can use the same wc command with ls command to count the number of files in a directory. I presume you are aware of the wc command for counting number of lines.