{"id":13784,"date":"2019-04-06T05:59:20","date_gmt":"2019-04-06T05:59:20","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/?p=13784"},"modified":"2019-04-06T06:35:14","modified_gmt":"2019-04-06T06:35:14","slug":"how-to-get-the-size-of-a-directory-in-linux","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/06\/how-to-get-the-size-of-a-directory-in-linux\/","title":{"rendered":"How to Get the Size of a Directory in Linux"},"content":{"rendered":"<div class=\"w-full h-full absolute m-auto pin bg-grey-lighter overflow-hidden\"><span style=\"font-size: 1rem;\">When listing the contents of a directory using the\u00a0<\/span><a style=\"font-size: 1rem;\" href=\"https:\/\/linuxize.com\/post\/how-to-list-files-in-linux-using-the-ls-command\/\">ls command<\/a><span style=\"font-size: 1rem;\">, you may have noticed that the size of the directories is almost always 4096 bytes (4 KB). That\u2019s the size of space on the disk that is used to store the meta information for the directory, not what it contains.<\/span><\/div>\n<p>The command you\u2019ll want to use to get the actual size of a directory is\u00a0<code>du<\/code>\u00a0which is short for \u201cdisk usage\u201d. We\u2019ll show you how to use this command.<\/p>\n<h2 id=\"getting-the-size-of-a-directory\" class=\"anchor\" aria-hidden=\"true\"><a href=\"https:\/\/linuxize.com\/post\/how-get-size-of-file-directory-linux\/#getting-the-size-of-a-directory\" data-scroll=\"\">Getting the Size of a Directory<\/a><\/h2>\n<p>The du command displays the amount of file space used by the specified files or directories. If the specified path is a directory, du will summarize disk usage of each file and subdirectory in that directory. If no path is specified, du will report the disk usage of the current working directory.<span id=\"ezoic-pub-ad-placeholder-138\" class=\"ezoic-adpicker-ad\"><\/span><span id=\"div-gpt-ad-linuxize_com-box-3-0\" class=\"ezoic-ad ezfound\" data-google-query-id=\"CKX-0u3ruuECFaMMrQYdaH4JZA\"><\/span><\/p>\n<p>If you run du without any option it will display the disk usage the specified directory and each of its subdirectories in bytes.<\/p>\n<p>In most cases, you would want to display only the space occupied by the directory in a human-readable format. For example, to get the total size of the\u00a0<code>\/var<\/code>\u00a0directory, you would run the following command:<span id=\"ezoic-pub-ad-placeholder-139\" class=\"ezoic-adpicker-ad\"><\/span><\/p>\n<div class=\"highlight\">\n<pre class=\"chroma console-bash\"><code class=\"language-console-bash\" data-lang=\"console-bash\"><span class=\"line\">sudo du -sh \/var<\/span><\/code><\/pre>\n<\/div>\n<p>The output will look something like this.<\/p>\n<div class=\"highlight\">\n<pre class=\"chroma\"><code class=\"language-output\" data-lang=\"output\">85G\t\/var<\/code><\/pre>\n<\/div>\n<p>Let\u2019s explain the command and its arguments:<span id=\"ezoic-pub-ad-placeholder-140\" class=\"ezoic-adpicker-ad\"><\/span><span id=\"div-gpt-ad-linuxize_com-medrectangle-3-0\" class=\"ezoic-ad ezfound\" data-google-query-id=\"CLixy_HruuECFa4FrQYdaxMNhQ\"><\/span><\/p>\n<ul>\n<li>The command starts with\u00a0<code>sudo<\/code>\u00a0because most of the files and directories inside the\u00a0<code>\/var<\/code>\u00a0directory are owned by the root user and are not readable by the regular users. If you omit sudo the du command will print \u201cdu: cannot read directory\u201d.<\/li>\n<li><code>s<\/code>\u00a0&#8211; Display only the total size of the specified directory, do not display file size totals for subdirectories.<\/li>\n<li><code>h<\/code>\u00a0&#8211; Print sizes in a human-readable format (<code>h<\/code>).<\/li>\n<li><code>\/var<\/code>\u00a0&#8211; The path to the directory you want to get the size.<\/li>\n<\/ul>\n<p>What if you want to display the disk usage of the first-level subdirectories. You have two options, the first one is to use the asterisk symbol as shown below which means \u201ceverything that doesn\u2019t start with a period (<code>.<\/code>)\u201c. The\u00a0<code>c<\/code>\u00a0switch tells du to print a grand total of all sizes:<\/p>\n<div class=\"highlight\">\n<pre class=\"chroma console-bash\"><code class=\"language-console-bash\" data-lang=\"console-bash\"><span class=\"line\">sudo du -shc \/var\/*<\/span><\/code><\/pre>\n<\/div>\n<div class=\"highlight\">\n<pre class=\"chroma\"><code class=\"language-output\" data-lang=\"output\">.0G\t\/var\/cache\r\n24K\t\/var\/db\r\n4.0K\t\/var\/empty\r\n4.0K\t\/var\/games\r\n77G\t\/var\/lib\r\n4.0K\t\/var\/local\r\n0\t\/var\/lock\r\n3.3G\t\/var\/log\r\n0\t\/var\/mail\r\n4.0K\t\/var\/opt\r\n0\t\/var\/run\r\n196K\t\/var\/spool\r\n28K\t\/var\/tmp\r\n85G\ttotal<\/code><\/pre>\n<\/div>\n<p>Another option is to use the\u00a0<code>--max-depth<\/code>\u00a0switch and specify the subdirectories level:<\/p>\n<div class=\"highlight\">\n<pre class=\"chroma console-bash\"><code class=\"language-console-bash\" data-lang=\"console-bash\"><span class=\"line\">sudo du -h --max-depth=1 \/var<\/span><\/code><\/pre>\n<\/div>\n<div class=\"highlight\">\n<pre class=\"chroma\"><code class=\"language-output\" data-lang=\"output\">77G\t  \/var\/lib\r\n24K\t  \/var\/db\r\n4.0K\t\/var\/empty\r\n4.0K\t\/var\/local\r\n4.0K\t\/var\/opt\r\n196K\t\/var\/spool\r\n4.0K\t\/var\/games\r\n3.3G\t\/var\/log\r\n5.0G\t\/var\/cache\r\n28K\t\/var\/tmp\r\n85G\t\/var\r\n85G\ttotal<\/code><\/pre>\n<\/div>\n<p>By default, the du utility shows the disk space used by the directory or file. The \u201capparent size\u201d of a file is how much data is actually in the file.<\/p>\n<p>To find the apparent size of a directory use the\u00a0<code>--apparent-size<\/code>\u00a0switch.<\/p>\n<div class=\"highlight\">\n<pre class=\"chroma console-bash\"><code class=\"language-console-bash\" data-lang=\"console-bash\"><span class=\"line\">sudo du -sh --apparent-size \/var<\/span><\/code><\/pre>\n<\/div>\n<p>When you transfer a directory via\u00a0<a href=\"https:\/\/linuxize.com\/post\/how-to-use-scp-command-to-securely-transfer-files\/\">SCP<\/a>,\u00a0<a href=\"https:\/\/linuxize.com\/post\/how-to-use-rsync-for-local-and-remote-data-transfer-and-synchronization\/\">Rsync<\/a>\u00a0or\u00a0<a href=\"https:\/\/linuxize.com\/post\/how-to-use-linux-sftp-command-to-transfer-files\/\">SFTP<\/a>\u00a0the amount of data that will be transferred over the network is the apparent size of the files. This is why the size of space on the disk that is used on the source when displayed with du (without\u00a0<code>--apparent-size<\/code>) will not be the same as the size on the target.<span id=\"ezoic-pub-ad-placeholder-142\" class=\"ezoic-adpicker-ad\"><\/span><span id=\"div-gpt-ad-linuxize_com-medrectangle-4-0\" class=\"ezoic-ad ezfound\" data-google-query-id=\"CLmN--_ruuECFckCrQYdpwQLRw\"><\/span><\/p>\n<p>The du command can also be combined with other commands with pipes. For example, to print the 5\u00a0<a href=\"https:\/\/linuxize.com\/post\/find-large-files-in-linux\/\">largest directories<\/a>\u00a0in the\u00a0<code>\/var<\/code>\u00a0directory you would use:<\/p>\n<div class=\"highlight\">\n<pre class=\"chroma console-bash\"><code class=\"language-console-bash\" data-lang=\"console-bash\"><span class=\"line\">sudo du -h \/var\/ | sort -rh | head -5<\/span><\/code><\/pre>\n<p><span class=\"code-copy button main small\" data-clipboard-text=\"sudo du -h \/var\/ | sort -rh | head -5\">Copy<\/span><\/p>\n<\/div>\n<div class=\"highlight\">\n<pre class=\"chroma\"><code class=\"language-output\" data-lang=\"output\">85G\t\/var\/\r\n77G\t\/var\/lib\r\n75G\t\/var\/lib\/libvirt\/images\r\n75G\t\/var\/lib\/libvirt\r\n5.0G\t\/var\/cache\/pacman\/pkg<\/code><\/pre>\n<\/div>\n<h2 id=\"conclusion\" class=\"anchor\" aria-hidden=\"true\"><a href=\"https:\/\/linuxize.com\/post\/how-get-size-of-file-directory-linux\/#conclusion\" data-scroll=\"\">Conclusion<\/a><\/h2>\n<p>In this tutorial, you learned how to get the size of a directory using the\u00a0<code>du<\/code>\u00a0command. If you have any question or remark, please leave a comment below.<\/p>\n<p><a href=\"http:\/\/lxer.com\/module\/newswire\/ext_link.php?rid=268067\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When listing the contents of a directory using the\u00a0ls command, you may have noticed that the size of the directories is almost always 4096 bytes (4 KB). That\u2019s the size of space on the disk that is used to store the meta information for the directory, not what it contains. The command you\u2019ll want to &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/06\/how-to-get-the-size-of-a-directory-in-linux\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to Get the Size of a Directory in Linux&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-13784","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13784","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=13784"}],"version-history":[{"count":2,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13784\/revisions"}],"predecessor-version":[{"id":13789,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13784\/revisions\/13789"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=13784"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=13784"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=13784"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}