{"id":17641,"date":"2022-06-06T11:08:38","date_gmt":"2022-06-06T11:08:38","guid":{"rendered":"https:\/\/linuxways.net\/?p=17641"},"modified":"2022-06-06T11:08:38","modified_gmt":"2022-06-06T11:08:38","slug":"how-to-count-files-in-a-directory-and-its-subdirectories-in-linux","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/centos\/how-to-count-files-in-a-directory-and-its-subdirectories-in-linux\/","title":{"rendered":"How to Count Files in a Directory and its Subdirectories in Linux"},"content":{"rendered":"<p>While navigating your Linux file system, you may come across directories containing a lot of files. Sometimes you need to find the number of files in those directories and their subdirectories in a Linux system. As manual counting is not a practical solution in this case. Therefore in this post, we will look at some other quick methods to count files in a directory and its subdirectories in a Linux OS.<\/p>\n<h2>Method#1 Using the find Command<\/h2>\n<p>The find command combined with the wc command can help you count the files recursively. The find command finds and lists all the files in a directory and its subdirectories and then the wc command counts the number of files. This way you can get the count of files in a directory recursively.<\/p>\n<p>Here is the syntax to count only the files in a directory recursively:<\/p>\n<pre>$ find &lt;path&gt; -type f | wc -l<\/pre>\n<p>For instance, to count all the files in the current directory, the command would be:<\/p>\n<pre>$ find -type f | wc -l<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"491\" height=\"70\" class=\"wp-image-17642\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17641-1.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17641-1.png 491w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17641-1-300x43.png 300w\" sizes=\"auto, (max-width: 491px) 100vw, 491px\" \/><\/p>\n<p>Similarly, to count only the files in the \u2018Documents\u2019 directory, the command would be:<\/p>\n<pre>$ find ~\/Documents -type f | wc -l<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"619\" height=\"67\" class=\"wp-image-17643\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17641-2.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17641-2.png 619w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17641-2-300x32.png 300w\" sizes=\"auto, (max-width: 619px) 100vw, 619px\" \/><\/p>\n<p>To count only the subdirectories within a directory, use &#8211;<em>type d<\/em> as follows:<\/p>\n<pre>$ find &lt;path&gt; -type d | wc -l<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"619\" height=\"68\" class=\"wp-image-17644\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17641-3.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17641-3.png 619w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17641-3-300x33.png 300w\" sizes=\"auto, (max-width: 619px) 100vw, 619px\" \/><\/p>\n<p>To include both the files and the subdirectories in the count, use the command below:<\/p>\n<pre>$ find &lt;path&gt; | wc -l<\/pre>\n<p>The find command also enables you to confine your search to specific directory levels. Below is the directory tree of our Documents directory.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"500\" height=\"269\" class=\"wp-image-17645\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17641-4.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17641-4.png 500w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17641-4-300x161.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>Now, for instance, if you want to count the files in a specific directory up to 3 levels (up to \u2018mydocs\u2019 directory), use the command line option \u2018-maxdepth 3\u2019:<\/p>\n<pre>$ find &lt;path&gt; -maxdepth 3 -type f| wc -l<\/pre>\n<p>This command will count the files up to 3 levels with the top level being the \u2018Documents\u2019 directory.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"636\" height=\"68\" class=\"wp-image-17646\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17641-5.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17641-5.png 636w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17641-5-300x32.png 300w\" sizes=\"auto, (max-width: 636px) 100vw, 636px\" \/><\/p>\n<p>Similarly, you can also start the count from a specific level. For instance, if you want to exclude the first 2 directories from the file count, you can use the command line option \u2018-mindepth 2\u2019. The \u2018-mindepth 2\u2019 will tell the find command to go 2 levels down before starting the search.<\/p>\n<pre>$ find &lt;path&gt; -mindepth 2 -type f | wc -l<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"646\" height=\"64\" class=\"wp-image-17647\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17641-6.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17641-6.png 646w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17641-6-300x30.png 300w\" sizes=\"auto, (max-width: 646px) 100vw, 646px\" \/><\/p>\n<h2>Method#2 Using the ls Command<\/h2>\n<p>The ls command in Linux is used for listing files and directories. Using the ls with the wc command, we can get the count of files in a specific directory. However, note that this count does not include the files inside the subdirectories.<\/p>\n<p>To find the number of files in a directory, pass its output to the wc command as follows:<\/p>\n<pre>$ ls &lt;path&gt; | wc -l<\/pre>\n<p>If you do not specify the directory path, it will count files in the current working directory,<\/p>\n<p>Here is the output of the ls command in our \u2018Documents\u2019 directory.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"667\" height=\"65\" class=\"wp-image-17648\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17641-7.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17641-7.png 667w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17641-7-300x29.png 300w\" sizes=\"auto, (max-width: 667px) 100vw, 667px\" \/><\/p>\n<p>Now to count the number of files in the \u2018Documents\u2019 directory, the command would be:<\/p>\n<pre>$ ls -a &lt;path&gt; | wc -l<\/pre>\n<p>The ls command will list the files in the specified directory while the wc command will count the number of files. So in the output, you will get the number of files including the subdirectories under the specified directory. Here note that this count will not be recursive as it will not count the files under the subdirectories.<\/p>\n<p>To exclude subdirectories and count only the files in a directory, use the command below:<\/p>\n<pre>$ ls -p &lt;path&gt; | grep -v \/ | wc -l<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"646\" height=\"68\" class=\"wp-image-17649\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17641-8.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17641-8.png 646w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17641-8-300x32.png 300w\" sizes=\"auto, (max-width: 646px) 100vw, 646px\" \/><\/p>\n<p>To count the hidden files too, use the command below:<\/p>\n<pre>$ ls -Ap &lt;path&gt; | grep -v \/ | grep \"^.\" | wc -l<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"711\" height=\"68\" class=\"wp-image-17650\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17641-9.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17641-9.png 711w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17641-9-300x29.png 300w\" sizes=\"auto, (max-width: 711px) 100vw, 711px\" \/><\/p>\n<h2>Method#3 Using the tree Command<\/h2>\n<p>The tree command also tells you the count of files and subdirectories under a directory. To count the files and directories under a specific directory, use the command below:<\/p>\n<pre>$ tree &lt;path&gt; | tail -1<\/pre>\n<p>In the output, you will find the count of files and directories under the specified directory.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"553\" height=\"69\" class=\"wp-image-17651\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17641-10.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17641-10.png 553w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17641-10-300x37.png 300w\" sizes=\"auto, (max-width: 553px) 100vw, 553px\" \/><\/p>\n<p>To include the hidden files too, use the -a flag with the tree command as follows:<\/p>\n<pre>$ tree -a &lt;path&gt; | tail -1<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"553\" height=\"65\" class=\"wp-image-17652\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17641-11.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17641-11.png 553w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17641-11-300x35.png 300w\" sizes=\"auto, (max-width: 553px) 100vw, 553px\" \/><\/p>\n<p>That\u2019s all! In this post, we have gone through how to count files in a directory and its subdirectories on Linux OS. We have discussed three different methods to count files in a directory which include the ls, find and tree commands.<\/p>","protected":false},"excerpt":{"rendered":"<p>While navigating your Linux file system, you may come across directories containing a lot of files. Sometimes you need to find the number of files in those directories&hellip;<\/p>","protected":false},"author":1,"featured_media":17653,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,4,891,5,83,165,2],"tags":[47,135],"class_list":["post-17641","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-centos","category-debian","category-manjaro","category-mint","category-opensuse","category-red-hat","category-ubuntu","tag-directories","tag-files-2"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/17641","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=17641"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/17641\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/17653"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=17641"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=17641"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=17641"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}