Analyse Your Disk Space Availability

Hard disks nowadays indeed come with lots of storage space. However, if you continuously add data without cleaning the unnecessary bits and bytes, some day you will surely run out of free space. I compile the LFY CD, and therefore need to download and test a lot of software. I always try to build the software from source. After a successful software installation, there is no need for the built binaries in the source directory. If I forget to clean them, after a few days, I run out of free space. So it becomes necessary for me to find a tool that eases my house-cleaning job. The du (disk usage) command and two GUI tools—KDirStat for KDE and Baobab for GNOME—solve my problems.

du tells me about disk usage

Let’s start with the du command, which summarises the disk usage of each file and directory on a disk. If you run it without any arguments, it displays the disk usage of files and subdirectories in the present working directory. Now, try the following commands instead (note that my present working directory is my desktop):

sandeep@linux-i7c8:~/Desktop> du -ch
4.1M    ./My Stuff/Blender 2.46 review/images
4.2M    ./My Stuff/Blender 2.46 review
5.9M    ./My Stuff/Swap/Avidemux
2.4M    ./My Stuff/Swap/SOng
5.1M    ./My Stuff/Swap/VLC
14M     ./My Stuff/Swap
27M     ./My Stuff
17M     ./PDF Bangalore
82M     .
82M     total

The -c option here gives the grand total, and the -h option presents the size in human-readable form. What if you want this du listing of the file sizes in an ascending order? Try the following command:

sandeep@linux-i7c8:~/Desktop> du -ch | sort -n
2.4M    ./My Stuff/Swap/SOng
4.1M    ./My Stuff/Blender 2.46 review/images
4.2M    ./My Stuff/Blender 2.46 review
5.1M    ./My Stuff/Swap/VLC
5.9M    ./My Stuff/Swap/Avidemux
14M     ./My Stuff/Swap
17M     ./PDF Bangalore
27M     ./My Stuff
82M     .
82M     total

Compare the above output with the previous one—do you notice that the order of the directory listing has changed now? Piping the previous output to the sort command helps achieve this with the aid of the -n (numerical-sort) switch, which reorders the output based on the file sizes provided by the du command.

In this manner you can check the contents of the largest directory and see if there are any unnecessary files there. If so, delete them to free up space.

Another handy command is df that you can use to check the disk space usage on all mounted file systems:

sandeep@linux-i7c8:~> df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1             5.4G  2.5G  2.6G  50% /
udev                  247M  180K  246M   1% /dev
/dev/sda3              28G   11G   18G  37% /home

Adding the -h flag to df presents the file sizes in a human readable form, viz. in KBs, MBs or GBs instead of 1K block sizes.

All this is pretty nifty, but let’s face it: it’s often difficult to work with command line tools when the disk space is large and the number of files are more. In this case the GUI tools such as KDirStat for KDE and Baobab for GNOME come to the rescue.

KDirStat for directory statistics

KDirStat is a graphical disk usage utility similar to the du command with some additional cleanup features. Most of the Linux distributions have this software included in their software repositories. If it is not installed by default, you can install it using the package manager of your distribution.

After launching it, you will be asked for the directory that you want to scan for disk usage. I have scanned the entire root filesystem (/) and the output is shown in Figure 1. The output contains the directories in a sub-tree structure, usage in both graphical and numerical form, the number of files and sub-directories, and the latest modified time within an entire directory tree.

Figure 1: KDirStat in action

Figure 1: KDirStat in action

KDirStat scans the directories on your mounted filesystem and also scans the FTP or Samba directories. It provides some predefined cleanup actions like compressing it to a tar.bz2 archive, deleting a file or a directory, or opening the directory in a terminal or in Konqueror. You can edit the clean-up actions or define your own set of actions. These settings are shown in Figure 2. If you are a systems administrator, you can even send an e-mail to the owner of the directory that’s consuming a lot of space, notifying him to clean up the unused files. The option is available under the Report menu.

Figure 2: KDirStat settings

Figure 2: KDirStat settings

I’d recommend you play around with this software to understand its various features and functionalities.

Baobab from the GNOME userland

If you are a GNOME user, you’ve something called Baobab (a.k.a Disk Usage Analyser) to analyse the disk usage available by default. You can run it from System→Administration→Disk Usage Analyser.

It’s similar to KDirStat. It scans the entire file system or a specific directory provided by you (local or remote). The output for the entire file system scan is shown in Figure 3. The output contains the directory tree, size and the total number of objects. It also displays the size for hard links that exist in a particular directory.

Figure 3: Filesystem disk usage scan using Baobab

Figure 3: Filesystem disk usage scan using Baobab

Tips to get some free space

If you install software from sources like I do, then to build it you obviously need to uncompress it and then install. After a successful installation, remove either the tar file or the uncompressed folder. Try to find out the old downloaded ISO images, etc, and delete them if they’re not useful. Burn the required ISOs to CD/DVD and then also delete them.

If you’re in charge of servers, I’m sure you know that a large amount of space is used for log files. Logs are very useful to track problems or debug issues. But most of the time they are unused. Check in your /var/log/ directory to find out the unused log files and compress them using gzip.

Well, that’s all for now! But remember, to get good systems performance we need to keep a balance between disk usage and the free space available. Even though you have a hard disk with a high storage capacity, some day you may run out of free space. So remove the unwanted files from the disk and get more free space.

  • http://twitter.com/rotovibes Jorge

    You can use
    du -ch | sort -h
    instead to sort sizes including units (K,M,G etc)

All published articles are released under Creative Commons Attribution-NonCommercial 3.0 Unported License, unless otherwise noted.
Open Source For You is powered by WordPress, which gladly sits on top of a CentOS-based LEMP stack.

Creative Commons License.