{"id":561,"date":"2012-03-09T00:42:12","date_gmt":"2012-03-09T05:42:12","guid":{"rendered":"http:\/\/blog.xfloyd.net\/?p=561"},"modified":"2012-03-09T00:43:16","modified_gmt":"2012-03-09T05:43:16","slug":"shell-du-command-tip-%e2%80%93-estimate-file-space-usage-and","status":"publish","type":"post","link":"https:\/\/xfloyd.net\/blog\/?p=561","title":{"rendered":"Shell du command tip \u2013 estimate file space usage and"},"content":{"rendered":"<p>The du command estimate file space usage and summarize disk usage of each<br \/>\nFILE, recursively for directories.<\/p>\n<p>It displays the file system block usage for each file argument and for each<br \/>\ndirectory in the file hierarchy rooted in each direc tory argument. If no file<br \/>\nis specified it will use current directory.<\/p>\n<h3>But why use du command?<\/h3>\n<p>You must be wondering why I\u2019m throwing out a light on du command. du is<br \/>\ncommonly used by system administrators to automate monitoring and notification<br \/>\nprograms that help prevent directories from becoming full.<\/p>\n<h3>du command Examples<!--more--><\/h3>\n<p>Type du to display usage in current directory :<br \/>\n<code>$<br \/>\n<strong>du<\/strong><\/code><\/p>\n<p>Pass -h option to display output in Byte, Kilobyte, Megabyte, Gigabyte,<br \/>\nTerabyte and Petabyte (Human-readable output)<br \/>\n<code>$ <strong>du<br \/>\n-h<\/strong><\/code><\/p>\n<p>Display the name and size of each png file in the \/ramdisk\/figs\/ directory as<br \/>\nwell as a total for all of the pngs:<br \/>\n<code>$ du -hc<br \/>\n\/ramdisk\/figs\/*.png<\/code><\/p>\n<p>Another useful option is -c which produce a grand total:<br \/>\n<code>$ du<br \/>\n-c<\/code><\/p>\n<p>Show the disk usage of the \/home\/vivek subdirectory:<br \/>\n<code>$ du<br \/>\n\/home\/vivek<\/code><\/p>\n<p>Show only a summary of the disk usage of the \/home\/vivek<br \/>\n<code>$ du -hs<br \/>\n\/home\/vivek<\/code><\/p>\n<p>Exclude files that match PATTERN. For example do not count *.obj or *.jpg<br \/>\nfiles:<\/p>\n<pre>$ du -h --exclude='*.obj'\r\n$ du -h --exclude='*.jpg'<\/pre>\n<p>A PATTERN is a shell pattern (not a regular perl or other expression). The<br \/>\npattern ? matches any one character, whereas * matches any string.<\/p>\n<h3>Pipes and filters with du<\/h3>\n<p>Now display everything sorted by filesize:<br \/>\n<code>$ du -sk .[A-z]* *| sort<br \/>\n-n<\/code><\/p>\n<p>Display screenful output at a time as du generate more output than can fit on<br \/>\nthe console \/ screen:<br \/>\n<code>$ du -h | less<\/code><\/p>\n<p>To find top 3 directories, enter :<br \/>\n<code>$ cd \/chroot<br \/>\n$ du -sk * | sort<br \/>\n-nr | head -3<\/code><\/p>\n<pre>4620348 var\r\n651972  home\r\n27896   usr\r\n21384   lib64<\/pre>\n<h3>Working without du<\/h3>\n<p>Finally here is one liner (without du command) that prints top 10 filesize in<br \/>\nMb (thanks to dreyser for submitting idea):<br \/>\n<code># find \/var -type f | xargs<br \/>\nls -s | sort -rn | awk '{size=$1\/1024; printf(\"%dMb %s\\n\", size,$2);}' | head<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The du command estimate file space usage and summarize disk usage of each FILE, recursively for directories. It displays the file system block usage for each file argument and for each directory in the file hierarchy rooted in each direc tory argument. If no file is specified it will use current directory. But why use [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"_links":{"self":[{"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/561"}],"collection":[{"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=561"}],"version-history":[{"count":3,"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/561\/revisions"}],"predecessor-version":[{"id":564,"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/561\/revisions\/564"}],"wp:attachment":[{"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=561"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=561"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=561"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}