{"id":27388,"date":"2018-06-22T14:07:41","date_gmt":"2018-06-22T14:07:41","guid":{"rendered":"http:\/\/www.sickgaming.net\/blog\/2018\/06\/22\/how-to-check-disk-space-on-linux-from-the-command-line\/"},"modified":"2018-06-22T14:07:41","modified_gmt":"2018-06-22T14:07:41","slug":"how-to-check-disk-space-on-linux-from-the-command-line","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2018\/06\/22\/how-to-check-disk-space-on-linux-from-the-command-line\/","title":{"rendered":"How to Check Disk Space on Linux from the Command Line"},"content":{"rendered":"<div><img decoding=\"async\" src=\"http:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2018\/06\/how-to-check-disk-space-on-linux-from-the-command-line.jpg\" class=\"ff-og-image-inserted\" \/><\/div>\n<p><span><span>Quick question: How much space do you have left on your drives? A little or a lot? Follow up question: Do you know how to find out? If you happen to use a GUI desktop (e.g., GNOME, KDE, Mate, Pantheon, etc.), the task is probably pretty simple. But what if you\u2019re looking at a headless server, with no GUI? Do you need to install tools for the task? The answer is a resounding no. All the necessary bits are already in place to help you find out exactly how much space remains on your drives. In fact, you have two very easy-to-use options at the ready. <\/span><\/span><\/p>\n<p><span><span>In this article, I\u2019ll demonstrate these tools. I\u2019ll be using <\/span><a href=\"https:\/\/elementary.io\/\"><span>Elementary OS<\/span><\/a><span>, which also includes a GUI option, but we\u2019re going to limit ourselves to the command line. The good news is these command-line tools are readily available for every Linux distribution. On my testing system, there are a number of attached drives (both internal and external). The commands used are agnostic to where a drive is plugged in; they only care that the drive is mounted and visible to the operating system.<\/span><\/span><\/p>\n<p><span><span>With that said, let\u2019s take a look at the tools.<\/span><\/span><\/p>\n<h3><span><span>df<\/span><\/span><\/h3>\n<p><span><span>The <\/span><span><em>df<\/em> <\/span><span>command is the tool I first used to discover drive space on Linux, way back in the 1990s. It\u2019s very simple in both usage and reporting. To this day, <\/span><span>df <\/span><span>is my go-to command for this task. This command has a few switches but, for basic reporting, you really only need one. That command is <\/span><em><span>df -H<\/span><\/em><span>. The <\/span><em><span>-H <\/span><\/em><span>switch is for human-readable format. The output of <\/span><em><span>df -H<\/span><\/em><span><em> <\/em>will report how much space is used, available, percentage used, and the mount point of every disk attached to your system (<\/span><span>Figure 1<\/span><span>).<\/span><\/span><\/p>\n<p><span><span>What if your list of drives is exceedingly long and you just want to view the space used on a single drive? With<em> <\/em><\/span><em><span>df<\/span><\/em><span>, that is possible. Let\u2019s take a look at how much space has been used up on our primary drive, located at <\/span><em><span>\/dev\/sda1<\/span><\/em><span>. To do that, issue the command:<\/span><\/span><\/p>\n<pre>\n<span><span>df -H \/dev\/sda1<\/span><\/span><\/pre>\n<p><span><span>The output will be limited to that one drive (<\/span><span>Figure 2<\/span><span>).<\/span><\/span><\/p>\n<p><span><span>You can also limit the reported fields shown in the <\/span><em><span>df <\/span><\/em><span>output. Available fields are:<\/span><\/span><\/p>\n<ul>\n<li>\n<p><span><span>source \u2014 the file system source<\/span><\/span><\/p>\n<\/li>\n<li>\n<p><span><span>size \u2014 total number of blocks<\/span><\/span><\/p>\n<\/li>\n<li>\n<p><span><span>used \u2014 spaced used on a drive<\/span><\/span><\/p>\n<\/li>\n<li>\n<p><span><span>avail \u2014 space available on a drive<\/span><\/span><\/p>\n<\/li>\n<li>\n<p><span><span>pcent \u2014 percent of used space, divided by total size<\/span><\/span><\/p>\n<\/li>\n<li>\n<p><span><span>target \u2014 mount point of a drive <\/span><\/span><\/p>\n<\/li>\n<\/ul>\n<p><span><span>Let\u2019s display the output of all our drives, showing only the size, used, and avail (or availability) fields. The command for this would be:<\/span><\/span><\/p>\n<pre>\n<span><span>df -H --output=size,used,avail<\/span><\/span><\/pre>\n<p><span><span>The output of this command is quite easy to read (<\/span><span>Figure 3<\/span><span>).<\/span><\/span><\/p>\n<p><span><span>The only caveat here is that we don\u2019t know the source of the output, so we\u2019d want to include source like so:<\/span><\/span><\/p>\n<pre>\n<span><span>df -H --output=source,size,used,avail<\/span><\/span><\/pre>\n<p><span><span>Now the output makes more sense (<\/span><span>Figure 4<\/span><span>).<\/span><\/span><\/p>\n<h3><span><span>du<\/span><\/span><\/h3>\n<p><span><span>Our next command is <\/span><em><span>du<\/span><\/em><span>. As you might expect, that stands for disk usage. The <\/span><span><em>du<\/em> <\/span><span>command is quite different to the <\/span><span>df<\/span><span> command, in that it reports on directories and not drives. Because of this, you\u2019ll want to know the names of directories to be checked. Let\u2019s say I have a directory containing virtual machine files on my machine. That directory is <\/span><em><span>\/media\/jack\/HALEY\/VIRTUALBOX<\/span><\/em><span>. If I want to find out how much space is used by that particular directory, I\u2019d issue the command:<\/span><\/span><\/p>\n<pre>\n<span><span>du -h \/media\/jack\/HALEY\/VIRTUALBOX<\/span><\/span><\/pre>\n<p><span><span>The output of the above command will display the size of every file in the directory (<\/span><span>Figure 5<\/span><span>). <\/span><\/span><\/p>\n<p><span><span>So far, this command isn\u2019t all that helpful. What if we want to know the total usage of a particular directory? Fortunately, <\/span><span>du <\/span><span>can handle that task. On the same directory, the command would be:<\/span><\/span><\/p>\n<pre>\n<span><span>du -sh \/media\/jack\/HALEY\/VIRTUALBOX\/<\/span><\/span><\/pre>\n<p><span><span>Now we know how much total space the files are using up in that directory (<\/span><span>Figure 6<\/span><span>).<\/span><\/span><\/p>\n<p><span><span>You can also use this command to see how much space is being used on all child directories of a parent, like so:<\/span><\/span><\/p>\n<pre>\n<span><span>du -h \/media\/jack\/HALEY<\/span><\/span><\/pre>\n<p><span><span>The output of this command (<\/span><span>Figure 7<\/span><span>) is a good way to find out what subdirectories are hogging up space on a drive.<\/span><\/span><\/p>\n<p><span><span>The <\/span><span>du <\/span><span>command is also a great tool to use in order to see a list of directories that are using the most disk space on your system. The way to do this is by piping the output of <\/span><span>du <\/span><span>to two other commands: <\/span><span>sort<\/span><span> and <\/span><span>head<\/span><span>. The command to find out the top 10 directories eating space on a drive would look something like this:<\/span><\/span><\/p>\n<pre>\n<span><span>du -a \/media\/jack | sort -n -r | head -n 10<\/span><\/span><\/pre>\n<p><span><span>The output would list out those directories, from largest to least offender (<\/span><span>Figure 8<\/span><span>).<\/span><\/span><\/p>\n<h3><span><span>Not as hard as you thought<\/span><\/span><\/h3>\n<p><span><span>Finding out how much space is being used on your Linux-attached drives is quite simple. As long as your drives are mounted to the Linux system, both <\/span><span>df <\/span><span>and <\/span><span>du<\/span><span> will do an outstanding job of reporting the necessary information. With <\/span><span>df<\/span><span> you can quickly see an overview of how much space is used on a disk and with <\/span><span>du<\/span><span> you can discover how much space is being used by specific directories. These two tools in combination should be considered must-know for every Linux administrator. <\/span><\/span><\/p>\n<p><span><span>And, in case you missed it, I recently showed how to <\/span><a href=\"https:\/\/www.linux.com\/learn\/5-commands-checking-memory-usage-linux\"><span>determine your memory usage on Linux<\/span><\/a><span>. Together, these tips will go a long way toward helping you successfully manage your Linux servers.<\/span><\/span><\/p>\n<p><em><span><span>Learn more about Linux through the free <\/span><a href=\"https:\/\/training.linuxfoundation.org\/linux-courses\/system-administration-training\/introduction-to-linux\"><span>&#8220;Introduction to Linux&#8221; <\/span><\/a><span>course from The Linux Foundation and edX.<\/span><\/span><\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Quick question: How much space do you have left on your drives? A little or a lot? Follow up question: Do you know how to find out? If you happen to use a GUI desktop (e.g., GNOME, KDE, Mate, Pantheon, etc.), the task is probably pretty simple. But what if you\u2019re looking at a headless [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":27389,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[],"class_list":["post-27388","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux-freebsd-unix"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/27388","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=27388"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/27388\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media\/27389"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=27388"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=27388"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=27388"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}