Your Linux servers are up and humming like champs. You’ve finally mastered the tools you need to make them do what you want and have found the only maintenance they need is the regular check for updates. However, have you bothered to check drive space on those servers? If not, do you have any guess as to how much free storage you have left? This is a particular task you need to know.

Good thing is pretty simple–even from the command line.

I want to show you how you can check for drive space using three different commands:

  • df – reports the amount of disk space used on a file system
  • du – reports the amount of space used by specific files
  • btrfs – reports the amount of space used by a btrfs file system mount point

Each of the above commands is used for a different purpose and can be used in conjunction with one another for tasks such as locating what files are taking up the most disk space.

Let’s take a look at how each command is used.

The df command

This particular command is the one I tend to lean on the most. When you issue the command df, it will report the statistics on your file system (Figure A).

Figure A

You don’t have to list out every connected drive on your machine. If you want less clutter, check the space for a specific drive. Say you need a report on /dev/sda, which is your primary drive on the server. Issue the command df /dev/sda and the report will only include that drive (Figure B).

Figure B

You can also have df report usage in a more human readable format, using the -h option, like so:

df -h

This will report usage in a format that is quicker to ascertain (Figure C).

Figure C

If you want to get even more specific with your output, you can tell df exactly what you want it to report using the –output option, like so:

df --output=source,used,avail /dev/sda

The above command will report only the source, used space, and available space for the /dev/sda drive. You can include the following options:

  • source – source of the device mount point
  • size – total number of blocks
  • used – total number of used blocks
  • avail – total number of available blocks
  • pcent – percentage of used space
  • target – mount point for the device

The du command

The du command allows you to see what directories are using your disk space. This makes it really easy to track down those directories that are eating up the largest portion of precious storage. Say, for instance, you’ve used the df command to discover that one drive is nearly full. What is taking up so much space? Let’s use the du command to find that out.

If you issue the du command with no options, it will report back every file in the working directory (as well as every file in subdirectories). Let’s say you need to find the top ten largest directories on your system. The du command will do this for you (but it will take quite some time, depending on how many drives and how much data you have on your system). The command for this would look something like this:

du -a / | sort -n -r | head -n 10

The above command takes the results of du and pipes them to the sort command, which then pipes those results to the head command. Once the report comes back (Figure D), you’ll see how much space the top ten directories are taking up.

Figure D

You can then get more specific by running the above command on one of the reported directories. For example, the following command will report which subdirectories are using up the most space in a directory that was reportedly gobbling up a significant amount of storage:

du -a /jlwallen/media/HALEY | sort -n -r | head -n 10

The more specific you get, the more specific your results.

The btrfs command

Now we’re going to examine disks that make use of the btrfs file system. In order to use this software, you will have to install the btrfs-tools package with one of the following commands (depending upon your distribution):

sudo apt install btrfs-tools
​sudo yum install btrfs-tools
​sudo dnf install btrfs-tools
​sudo zypper install btrfs-tools

The btrfs command works with mount points instead of device names. So let’s say you need to find out how much space is being used on /media/jlwallen/AUDIO. The command for this would be:

btrfs fi df /media/jlwallen/AUDIO

The output for the above command (Figure E) would tell you how much space is being used by that filesystem.

Figure E

As to the output of the btrfs command:

  • Column 1 shows the type of item being stored (Data, System, Metadata)
  • Column 2 shows whether a single copy of each item is stored (single), or whether two copies of each item are stored (DUP)
  • Columns 3 and 4 show the total and used space

For more information on how to use the btrfs command, run man btrfs to read the manual page.

Know your system

With these three commands (assuming your machine(s) makes use of the btrfs file system for the third command), you can know exactly how much space remains on your connected drives and even how much space is being used by certain directories. These are great commands to know, especially if you’re working on GUI-less servers.

Subscribe to the Developer Insider Newsletter

From the hottest programming languages to commentary on the Linux OS, get the developer and open source news and tips you need to know. Delivered Tuesdays and Thursdays

Subscribe to the Developer Insider Newsletter

From the hottest programming languages to commentary on the Linux OS, get the developer and open source news and tips you need to know. Delivered Tuesdays and Thursdays