Scanning used storage

Almost everyone experiences a situation where disk space seems to vanish, with no clear indication as to what is taking up all the space. There are multiple ways in which you can troubleshoot what in particular is eating your hard drive space for breakfast. In order to see an overview of your mounted filesystems as well as their used and free space, execute the df command. Using -h with df is easier to read for most people, as it will show used space in MB and GB:

df -h

Armed with that information, you'll know exactly what device is being used up and what volumes to focus your attention on. But the df command doesn't actually tell you what is using up all the space; it only gives you an overview of the current situation.

Next up is du. The du command, which can also be paired with -h for the same reason, shows you how much space is being used in a directory. All you would need to do is to cd into the directory you wish to check, and then run du -h. For even easier to read output, run the following in a directory:

du -hsc *

Breaking down that command, we have -h parameter that we already know makes the output easier to read. The -s parameter shows only a total and -c will present you with a grand total at the end. Since we used an asterisk with the command, it will run du -hsc against each subdirectory contained within the current one. With this command, you can determine which directories in your current working directory are using up the most space.

However, it gets even better than that. As useful as du -hsc * is, you still have to run it manually for each subdirectory. There are ways to use it to scan deeper, but du is only useful for an overview summary. An even better way is to install ncdu. The ncdu command is not a graphical utility in that it doesn't require a graphical desktop environment. But it's so easy to use; you may think that it actually was a graphical utility. Once kicked off against a particular directory, it does a deep dive and allows you to actually traverse the filesystem tree from that point and follow what is using up all your space straight down to the culprit.

You don't need to be the root user or have sudo permission to utilize ncdu, but keep in mind ncdu can only scan directories that its calling user has permission to access. In some cases, you may need to run it as root to get around that. The basic usage of ncdu is simple; call ncdu along with a path for it to scan. For example, you can scan your entire filesystem or a section of it:

ncdu /
Scanning used storage

Scanning the root filesystem of a CentOS system with ncdu

It's important to note that by default ncdu will scan everything within the directory you give it, including anything that may be mounted. An example of this can be mounted NFS shares or external disks, but you may not want external mounts to factor into the results. Thankfully, this is as easy as presenting the -x option to ncdu, which tells it to ignore anything you have mounted when you run your scan:

ncdu -x /

Once the scan is finished, you can traverse the results by pressing up and down keys on your keyboard, and press Enter to change into a directory. From within ncdu itself, you can even delete files without having to run any extra commands by simply pressing D. This way, you can do your auditing and cleanup from the same tool.

Feel free to run ncdu on your own systems and interrogate where your free space is going. Unless you actually start deleting things, it's harmless and can show you some potential items you may want to clean up. On actual servers, ncdu is quite useful in troubleshooting where your disk space is going.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset