- Classic SysAdmin: How to Check Disk Space on Linux from the Command Line
- df
- du
- Not as hard as you thought
- 12 Useful “df” Commands to Check Disk Space in Linux
- 1. Check File System Disk Space Usage
- 2. Display Information of all File System Disk Space Usage
- 3. Show Disk Space Usage in Human Readable Format
- 4. Display Information of /home File System
- 5. Display Information of File System in Bytes
- 6. Display Information of File System in MB
- 7. Display Information of File System in GB
- 8. Display File System Inodes
- 9. Display File System Type
- 10. Include Certain File System Type
- 11. Exclude Certain File System Type
- 12. Display Information of df Command.
Classic SysAdmin: How to Check Disk Space on Linux from the Command Line
This is a classic article written by Jack Wallen from the Linux.com archives. For more great SysAdmin tips and techniques check out our free intro to Linux course.
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’re 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.
In this article, I’ll demonstrate these tools. I’ll be using Elementary OS, which also includes a GUI option, but we’re 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.
With that said, let’s take a look at the tools.
df
The df command is the tool I first used to discover drive space on Linux, way back in the 1990s. It’s very simple in both usage and reporting. To this day, df 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 df -H. The -H switch is for human-readable format. The output of df -H will report how much space is used, available, percentage used, and the mount point of every disk attached to your system (Figure 1).
What if your list of drives is exceedingly long and you just want to view the space used on a single drive? With df, that is possible. Let’s take a look at how much space has been used up on our primary drive, located at /dev/sda1. To do that, issue the command:
The output will be limited to that one drive (Figure 2).
You can also limit the reported fields shown in the df output. Available fields are:
- source — the file system source
- size — total number of blocks
- used — spaced used on a drive
- avail — space available on a drive
- pcent — percent of used space, divided by total size
- target — mount point of a drive
Let’s display the output of all our drives, showing only the size, used, and avail (or availability) fields. The command for this would be:
The output of this command is quite easy to read (Figure 3).
The only caveat here is that we don’t know the source of the output, so we’d want to include source like so:
df -H --output=source,size,used,avail
Now the output makes more sense (Figure 4).
du
Our next command is du. As you might expect, that stands for disk usage. The du command is quite different to the df command, in that it reports on directories and not drives. Because of this, you’ll want to know the names of directories to be checked. Let’s say I have a directory containing virtual machine files on my machine. That directory is /media/jack/HALEY/VIRTUALBOX. If I want to find out how much space is used by that particular directory, I’d issue the command:
du -h /media/jack/HALEY/VIRTUALBOX
The output of the above command will display the size of every file in the directory (Figure 5).
So far, this command isn’t all that helpful. What if we want to know the total usage of a particular directory? Fortunately, du can handle that task. On the same directory, the command would be:
du -sh /media/jack/HALEY/VIRTUALBOX/
Now we know how much total space the files are using up in that directory (Figure 6).
You can also use this command to see how much space is being used on all child directories of a parent, like so:
The output of this command (Figure 7) is a good way to find out what subdirectories are hogging up space on a drive.
The du 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 du to two other commands: sort and head. The command to find out the top 10 directories eating space on a drive would look something like this:
du -a /media/jack | sort -n -r | head -n 10
The output would list out those directories, from largest to least offender (Figure 8).
Not as hard as you thought
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 df and du will do an outstanding job of reporting the necessary information. With df you can quickly see an overview of how much space is used on a disk and with du 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.
And, in case you missed it, I recently showed how to determine your memory usage on Linux. Together, these tips will go a long way toward helping you successfully manage your Linux servers.
12 Useful “df” Commands to Check Disk Space in Linux
On the internet, you will find plenty of tools for checking disk space utilization in Linux. However, Linux has a strong built-in utility called ‘df‘.
The ‘df‘ command stands for “disk filesystem“, it is used to get a full summary of available and used disk space usage of the file system on the Linux system.
Using ‘ -h ‘ parameter with (df -h) will show the file system disk space statistics in “human-readable” format, means it gives the details in bytes, megabytes, and gigabyte.
This article explains a way to get the full information of Linux disk space usage with the help of the ‘df‘ command with their practical examples. So, you could better understand the usage of the df command in Linux.
1. Check File System Disk Space Usage
The “df” command displays the information of device name, total blocks, total disk space, used disk space, available disk space, and mount points on a file system.
[[email protected] ~]# df Filesystem 1K-blocks Used Available Use% Mounted on /dev/cciss/c0d0p2 78361192 23185840 51130588 32% / /dev/cciss/c0d0p5 24797380 22273432 1243972 95% /home /dev/cciss/c0d0p3 29753588 25503792 2713984 91% /data /dev/cciss/c0d0p1 295561 21531 258770 8% /boot tmpfs 257476 0 257476 0% /dev/shm
2. Display Information of all File System Disk Space Usage
The same as above, but it also displays information of dummy file systems along with all the file system disk usage and their memory utilization.
[[email protected] ~]# df -a Filesystem 1K-blocks Used Available Use% Mounted on /dev/cciss/c0d0p2 78361192 23186116 51130312 32% / proc 0 0 0 - /proc sysfs 0 0 0 - /sys devpts 0 0 0 - /dev/pts /dev/cciss/c0d0p5 24797380 22273432 1243972 95% /home /dev/cciss/c0d0p3 29753588 25503792 2713984 91% /data /dev/cciss/c0d0p1 295561 21531 258770 8% /boot tmpfs 257476 0 257476 0% /dev/shm none 0 0 0 - /proc/sys/fs/binfmt_misc sunrpc 0 0 0 - /var/lib/nfs/rpc_pipefs
3. Show Disk Space Usage in Human Readable Format
Have you noticed that the above commands display information in bytes, which is not readable at all because we are in a habit of reading the sizes in megabytes, gigabytes, etc. as it makes it very easy to understand and remember.
The df command provides an option to display sizes in Human Readable formats by using ‘-h’ (prints the results in human-readable format (e.g., 1K 2M 3G)).
[[email protected] ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/cciss/c0d0p2 75G 23G 49G 32% / /dev/cciss/c0d0p5 24G 22G 1.2G 95% /home /dev/cciss/c0d0p3 29G 25G 2.6G 91% /data /dev/cciss/c0d0p1 289M 22M 253M 8% /boot tmpfs 252M 0 252M 0% /dev/shm
4. Display Information of /home File System
To see the information of only device /home file systems in human-readable format use the following command.
[[email protected] ~]# df -hT /home Filesystem Type Size Used Avail Use% Mounted on /dev/cciss/c0d0p5 ext3 24G 22G 1.2G 95% /home
5. Display Information of File System in Bytes
To display all file system information and usage in 1024-byte blocks, use the option ‘ -k ‘ (e.g. —block-size=1K ) as follows.
[[email protected] ~]# df -k Filesystem 1K-blocks Used Available Use% Mounted on /dev/cciss/c0d0p2 78361192 23187212 51129216 32% / /dev/cciss/c0d0p5 24797380 22273432 1243972 95% /home /dev/cciss/c0d0p3 29753588 25503792 2713984 91% /data /dev/cciss/c0d0p1 295561 21531 258770 8% /boot tmpfs 257476 0 257476 0% /dev/shm
6. Display Information of File System in MB
To display information of all file system usage in MB (MegaByte) use the option ‘ -m ‘.
[[email protected] ~]# df -m Filesystem 1M-blocks Used Available Use% Mounted on /dev/cciss/c0d0p2 76525 22644 49931 32% / /dev/cciss/c0d0p5 24217 21752 1215 95% /home /dev/cciss/c0d0p3 29057 24907 2651 91% /data /dev/cciss/c0d0p1 289 22 253 8% /boot tmpfs 252 0 252 0% /dev/shm
7. Display Information of File System in GB
To display information of all file system statistics in GB (Gigabyte) use the option as ‘df -h‘.
[[email protected] ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/cciss/c0d0p2 75G 23G 49G 32% / /dev/cciss/c0d0p5 24G 22G 1.2G 95% /home /dev/cciss/c0d0p3 29G 25G 2.6G 91% /data /dev/cciss/c0d0p1 289M 22M 253M 8% /boot tmpfs 252M 0 252M 0% /dev/shm
8. Display File System Inodes
Using ‘ -i ‘ switch will display the information of a number of used inodes and their percentage for the file system.
[[email protected] ~]# df -i Filesystem Inodes IUsed IFree IUse% Mounted on /dev/cciss/c0d0p2 20230848 133143 20097705 1% / /dev/cciss/c0d0p5 6403712 798613 5605099 13% /home /dev/cciss/c0d0p3 7685440 1388241 6297199 19% /data /dev/cciss/c0d0p1 76304 40 76264 1% /boot tmpfs 64369 1 64368 1% /dev/shm
9. Display File System Type
If you notice all the above commands output, you will see there is no Linux file system type mentioned in the results. To check the file system type of your system use the option ‘ T ‘. It will display file system type along with other information.
[[email protected] ~]# df -T Filesystem Type 1K-blocks Used Available Use% Mounted on /dev/cciss/c0d0p2 ext3 78361192 23188812 51127616 32% / /dev/cciss/c0d0p5 ext3 24797380 22273432 1243972 95% /home /dev/cciss/c0d0p3 ext3 29753588 25503792 2713984 91% /data /dev/cciss/c0d0p1 ext3 295561 21531 258770 8% /boot tmpfs tmpfs 257476 0 257476 0% /dev/shm
10. Include Certain File System Type
If you want to display a certain file system type use the ‘ -t ‘ option. For example, the following command will only display the ext3 file system.
[[email protected] ~]# df -t ext3 Filesystem 1K-blocks Used Available Use% Mounted on /dev/cciss/c0d0p2 78361192 23190072 51126356 32% / /dev/cciss/c0d0p5 24797380 22273432 1243972 95% /home /dev/cciss/c0d0p3 29753588 25503792 2713984 91% /data /dev/cciss/c0d0p1 295561 21531 258770 8% /boot
11. Exclude Certain File System Type
If you want to display a file system type that doesn’t belongs to the ext3 type use the option ‘ -x ‘. For example, the following command will only display other file systems types other than ext3.
[[email protected] ~]# df -x ext3 Filesystem 1K-blocks Used Available Use% Mounted on tmpfs 257476 0 257476 0% /dev/shm
12. Display Information of df Command.
Using ‘—help ‘ switch will display a list of available option that is used with df command.
[[email protected] ~]# df --help Usage: df [OPTION]. [FILE]. Show information about the file system on which each FILE resides, or all file systems by default. Mandatory arguments to long options are mandatory for short options too. -a, --all include dummy file systems -B, --block-size=SIZE use SIZE-byte blocks -h, --human-readable print sizes in human readable format (e.g., 1K 234M 2G) -H, --si likewise, but use powers of 1000 not 1024 -i, --inodes list inode information instead of block usage -k like --block-size=1K -l, --local limit listing to local file systems --no-sync do not invoke sync before getting usage info (default) -P, --portability use the POSIX output format --sync invoke sync before getting usage info -t, --type=TYPE limit listing to file systems of type TYPE -T, --print-type print file system type -x, --exclude-type=TYPE limit listing to file systems, not of type TYPE -v (ignored) --help display this help and exit --version output version information and exit SIZE may be (or maybe an integer optionally followed by) one of the following: kB 1000, K 1024, MB 1000*1000, M 1024*1024, and so on for G, T, P, E, Z, Y. Report bugs to [email protected]>.