{"id":200,"date":"2024-01-19T02:14:33","date_gmt":"2024-01-19T02:14:33","guid":{"rendered":"https:\/\/buyandhost.in\/blog\/?p=200"},"modified":"2024-01-19T02:49:34","modified_gmt":"2024-01-19T02:49:34","slug":"count-files-directories-in-linux","status":"publish","type":"post","link":"https:\/\/buyandhost.in\/blog\/count-files-directories-in-linux\/","title":{"rendered":"How to Count\u00a0files &#038; directories in Linux"},"content":{"rendered":"\n<p>In <a href=\"https:\/\/www.linux.org\/\" data-type=\"link\" data-id=\"https:\/\/www.linux.org\/\" target=\"_blank\" rel=\"noopener\">Linux<\/a>, you can count the number of files and directories in a directory using the <code>ls<\/code> command in combination with other commands such as <code>wc<\/code> and <code>grep<\/code>.<\/p>\n\n\n\n<p>The easiest way to count number of files and subdirectories in a directory using a\u00a0<code>tree<\/code>\u00a0command, which is best known for displaying files and directories in tree-like form.<\/p>\n\n\n\n<p>Counting files and directories on a <a href=\"https:\/\/buyandhost.in\/vps-hosting.html\" data-type=\"link\" data-id=\"https:\/\/buyandhost.in\/vps-hosting.html\">web hosting server<\/a> involves using command-line tools, just as you would on a local Linux machine. <\/p>\n\n\n\n<p>You may have access to the server through SSH (Secure Shell) or a web-based terminal provided by your hosting provider. Here&#8217;s a basic guide using SSH<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/files-1-1024x576.jpg\" alt=\"\" class=\"wp-image-230\" srcset=\"https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/files-1-1024x576.jpg 1024w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/files-1-300x169.jpg 300w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/files-1-768x432.jpg 768w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/files-1.jpg 1170w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\"><h2>Table of Contents<\/h2><nav><ul><li><a href=\"#count-files-directories-for-inode-usage\">Count files &amp; directories for inode usage<\/a><ul><li><a href=\"#count-directories\">Count directories<\/a><\/li><li><a href=\"#count-files\">Count files<\/a><\/li><\/ul><\/li><li><a href=\"#count-files-directories-for-var-log\">Count files &amp; directories for var\/log <\/a><ul><li><a href=\"#count-directories-1\">Count directories<\/a><\/li><li><a href=\"#count-files-2\">Count files<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"count-files-directories-for-inode-usage\">Count files &amp; directories for inode usage<\/h2>\n\n\n\n<p>If you have the <code>tree<\/code> command installed, you can use it to count files and directories based on inode usage. <\/p>\n\n\n\n<p>The <code>tree<\/code> command provides a visual representation of the directory tree, and with the <code>-i<\/code> option, it shows inode numbers for each file and directory. <\/p>\n\n\n\n<p>You can then use <code>grep<\/code> and <code>wc<\/code> to count the number of files and directories<\/p>\n\n\n\n<p>Although you can always enable quotas to restrict disk space and inode usage to avoid user abuse, this command may be useful anyway. By default, the current working directory is assumed if no arguments are given<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tree -iLf 1<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"166\" src=\"https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-1024x166.png\" alt=\"\" class=\"wp-image-202\" srcset=\"https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-1024x166.png 1024w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-300x49.png 300w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-768x124.png 768w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image.png 1101w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"count-directories\">Count directories<\/h3>\n\n\n\n<p>You can count directories using below command<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tree -i -f -F | grep \"\/$\" | wc -l<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"53\" src=\"https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-3-1024x53.png\" alt=\"\" class=\"wp-image-218\" srcset=\"https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-3-1024x53.png 1024w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-3-300x16.png 300w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-3-768x40.png 768w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-3.png 1253w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"count-files\">Count files<\/h3>\n\n\n\n<p>You can count files using below command<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tree -i -f -F | grep -v \"\/$\" | wc -l<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"45\" src=\"https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-4-1024x45.png\" alt=\"\" class=\"wp-image-219\" srcset=\"https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-4-1024x45.png 1024w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-4-300x13.png 300w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-4-768x34.png 768w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-4.png 1291w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>tree -i -f -F<\/code>: This command generates a directory tree with inode numbers (<code>-i<\/code>), displaying the full path (<code>-f<\/code>), and appending a &#8216;\/&#8217; to directory names (<code>-F<\/code>).<\/li>\n\n\n\n<li>For counting directories:\n<ul class=\"wp-block-list\">\n<li><code>grep \"\/$\"<\/code>: This filters the output to include only lines ending with a &#8216;\/&#8217;. Directories in the tree are marked with a trailing &#8216;\/&#8217;.<\/li>\n\n\n\n<li><code>wc -l<\/code>: This counts the number of lines, giving you the count of directories.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>For counting files:\n<ul class=\"wp-block-list\">\n<li><code>grep -v \"\/$\"<\/code>: This filters the output to exclude lines ending with a &#8216;\/&#8217;. This includes regular files since they don&#8217;t have a trailing &#8216;\/&#8217;.<\/li>\n\n\n\n<li><code>wc -l<\/code>: This counts the number of lines, providing the count of files.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p>These commands give you separate counts for directories and files based on their inode usage in the specified directory and its subdirectories. Adjust the starting directory as needed.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"count-files-directories-for-var-log\">Count files &amp; directories for var\/log <\/h2>\n\n\n\n<p>If you want to view the same information for \/var\/log, do:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tree -iLf 1 \/var\/log<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"521\" src=\"https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-8-1024x521.png\" alt=\"\" class=\"wp-image-237\" srcset=\"https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-8-1024x521.png 1024w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-8-300x153.png 300w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-8-768x390.png 768w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-8.png 1123w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Type the command below to view information about files and subdirectories in the directory ISOs<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tree -iLf 1 ISOs<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"971\" height=\"124\" src=\"https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-2.png\" alt=\"\" class=\"wp-image-205\" srcset=\"https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-2.png 971w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-2-300x38.png 300w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-2-768x98.png 768w\" sizes=\"auto, (max-width: 971px) 100vw, 971px\" \/><\/figure>\n\n\n\n<p>Explaining&nbsp;<code>tree<\/code>&nbsp;options used in the command above:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>-i<\/code>&nbsp;\u2013 its a graphical option that enables tree to print out indentation lines<\/li>\n\n\n\n<li><code>-L<\/code>&nbsp;\u2013 specifies the level of depth of the directory tree to be displayed, which in the case above is&nbsp;<strong>1<\/strong><\/li>\n\n\n\n<li><code>-f<\/code>&nbsp;\u2013 makes tree print the full path prefix for every file<\/li>\n<\/ol>\n\n\n\n<p>As you can view from the image above, after listing all the files and subdirectories,&nbsp;<code>tree<\/code>&nbsp;shows you the total number of directories and files in the directory you specified.<\/p>\n\n\n\n<p>To count files and directories in the <code>\/var\/log<\/code> directory based on inode usage using the <code>tree<\/code> command, you can use the following commands:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"count-directories-1\">Count directories<\/h3>\n\n\n\n<p>You can count directories for \/var\/log using below command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tree -i -f -F \/var\/log | grep \"\/$\" | wc -l<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"45\" src=\"https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-5-1024x45.png\" alt=\"\" class=\"wp-image-222\" srcset=\"https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-5-1024x45.png 1024w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-5-300x13.png 300w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-5-768x34.png 768w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-5.png 1278w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"count-files-2\">Count files<\/h3>\n\n\n\n<p>You can count files for \/var\/log using below command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tree -i -f -F \/var\/log | grep -v \"\/$\" | wc -l<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"48\" src=\"https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-6-1024x48.png\" alt=\"\" class=\"wp-image-223\" srcset=\"https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-6-1024x48.png 1024w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-6-300x14.png 300w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-6-768x36.png 768w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-6-1320x62.png 1320w, https:\/\/buyandhost.in\/blog\/wp-content\/uploads\/2024\/01\/image-6.png 1327w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>tree -i -f -F \/var\/log<\/code>: This command generates a directory tree with inode numbers (<code>-i<\/code>), displaying the full path (<code>-f<\/code>), and appending a &#8216;\/&#8217; to directory names (<code>-F<\/code>) for the <code>\/var\/log<\/code> directory.<\/li>\n\n\n\n<li>For counting directories:\n<ul class=\"wp-block-list\">\n<li><code>grep \"\/$\"<\/code>: This filters the output to include only lines ending with a &#8216;\/&#8217;. Directories in the tree are marked with a trailing &#8216;\/&#8217;.<\/li>\n\n\n\n<li><code>wc -l<\/code>: This counts the number of lines, giving you the count of directories.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>For counting files:\n<ul class=\"wp-block-list\">\n<li><code>grep -v \"\/$\"<\/code>: This filters the output to exclude lines ending with a &#8216;\/&#8217;. This includes regular files since they don&#8217;t have a trailing &#8216;\/&#8217;.<\/li>\n\n\n\n<li><code>wc -l<\/code>: This counts the number of lines, providing the count of files.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p>These commands will give you separate counts for directories and files based on their inode usage in the <code>\/var\/log<\/code> directory. Adjust the starting directory as needed.<\/p>\n\n\n\n<p>You can refer to the&nbsp;<strong>tree man page<\/strong>&nbsp;to discover more useful options, some configuration files and environment variables to better understand how it works.<\/p>\n\n\n\n<p>We covered an important tip that can help you use the&nbsp;tree utility in a different way as compared to its traditional use, for displaying files and directories in a tree-like form.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Linux, you can count the number of files and directories in a directory using the ls command in combination with other commands such as wc and grep. The easiest way to count number of files and subdirectories in a directory using a\u00a0tree\u00a0command, which is best known for displaying files and directories in tree-like form. [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":228,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[14,12],"class_list":["post-200","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","tag-count","tag-files"],"_links":{"self":[{"href":"https:\/\/buyandhost.in\/blog\/wp-json\/wp\/v2\/posts\/200","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buyandhost.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buyandhost.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buyandhost.in\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/buyandhost.in\/blog\/wp-json\/wp\/v2\/comments?post=200"}],"version-history":[{"count":18,"href":"https:\/\/buyandhost.in\/blog\/wp-json\/wp\/v2\/posts\/200\/revisions"}],"predecessor-version":[{"id":238,"href":"https:\/\/buyandhost.in\/blog\/wp-json\/wp\/v2\/posts\/200\/revisions\/238"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buyandhost.in\/blog\/wp-json\/wp\/v2\/media\/228"}],"wp:attachment":[{"href":"https:\/\/buyandhost.in\/blog\/wp-json\/wp\/v2\/media?parent=200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buyandhost.in\/blog\/wp-json\/wp\/v2\/categories?post=200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buyandhost.in\/blog\/wp-json\/wp\/v2\/tags?post=200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}