How to Check Disk Space and Usage in Linux
Use code KB4KDO0L9 to receive a 10% recurring discount on any server.
When managing your servers, taking charge of system resources is of utmost importance. There are many advantages and benefits of Linux which makes it one of the most popular operating systems out there. Every server has a lot of resources like memory, computing power, etc. One of the most important system resources is the hard disk space. To manage your server effectively and ensure no unwanted disruption, you need to manage your disk space frequently. When your system runs out of space, the application or database hosted on the system will crash. Recovery from a crash might take some time, and you may lose important data which wasn’t flushed to disk because the disk ran out of space. You can learn about ServerMania’s Linux dedicated servers and Linux server costs on our official blog.
The Linux ecosystem provides you with the df (which stands for disk free) command to find disk space utilization information from the command line. Of course, in the Linux-based GUI operating systems like Ubuntu, you can get the disk space available from the GUI (using the Disk Usage Analyzer tool) and the command line.
Check Disk Space in Linux using the command df (disk free)
The barebones version of the df command will show you the space occupied by different drives in 1KB (kilobyte) blocks. In the output, you will be provided with mount point information, available and used space, and the percentage occupancy of the disk. To check disk space for all the filesystems and their disk usage, you need to log into your Linux terminal and type the following command:
[root@centos ~]# df File system 1K-blocks Used Available Use% Mounted on devtmpfs 8086916 0 8086916 0% /dev tmpfs 8102060 0 8102060 0% /dev/shm tmpfs 8102060 16776 8085284 1% /run tmpfs 8102060 0 8102060 0% /sys/fs/cgroup /dev/vda1 330147436 3237576 310116312 2% / tmpfs 1620412 0 1620412 0% /run/user/0
You can use many options with the df command to check disk space:
usage: df [-b | -H | -h | -k | -m | -g | -P] [-ailn] [-T type] [-t] [file system ...]
Let’s look at some important ones.
Disk Space in Human Readable Format
The first difficultly one faces while reading disk space from the df command is that the disk space, by default, is presented in bytes. To get this data in a human-readable format, i.e., in KB, MB, GB, and TBs, use -h (for powers of 1024) or -H (for powers of 1000) options as shown below:
[root@centos ~]# df -h File system Size Used Avail Use% Mounted on devtmpfs 7.8G 0 7.8G 0% /dev tmpfs 7.8G 0 7.8G 0% /dev/shm tmpfs 7.8G 17M 7.8G 1% /run tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup /dev/vda1 315G 3.1G 296G 2% / tmpfs 1.6G 0 1.6G 0% /run/user/0
Summary with Total Available and Total Used Disk Space
Sometimes it is handy to have the total available and total used disk space for reporting and alerting purposes. Rather than running this command directly from the CLI on your operating system, you can also get this data using an alerting service like Nagios or DataDog.
[root@centos ~]# df --total File system 1K-blocks Used Available Use% Mounted on devtmpfs 8086916 0 8086916 0% /dev tmpfs 8102060 0 8102060 0% /dev/shm tmpfs 8102060 16776 8085284 1% /run tmpfs 8102060 0 8102060 0% /sys/fs/cgroup /dev/vda1 330147436 3237576 310116312 2% / tmpfs 1620412 0 1620412 0% /run/user/0 total 364160944 3254352 344113044 1% -
Disk Space with the Filesystem Type
This command helps you fetch the filesystem type, along with the usual information offered by the df command:
[root@centos ~]# df -h -T File system Type Size Used Avail Use% Mounted on devtmpfs devtmpfs 7.8G 0 7.8G 0% /dev tmpfs tmpfs 7.8G 0 7.8G 0% /dev/shm tmpfs tmpfs 7.8G 17M 7.8G 1% /run tmpfs tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup /dev/vda1 ext4 315G 3.1G 296G 2% / tmpfs tmpfs 1.6G 0 1.6G 0% /run/user/0
Disk Space in Inodes Instead of 1K Blocks
If you have an application that generates a lot of tiny files and stores them locally on disk, in addition to the disk space, you might want to track the Inode usage with the following command:
[root@centos ~]# df -hi File system Inodes IUsed IFree IUse% Mounted on devtmpfs 2.0M 347 2.0M 1% /dev tmpfs 2.0M 1 2.0M 1% /dev/shm tmpfs 2.0M 436 2.0M 1% /run tmpfs 2.0M 17 2.0M 1% /sys/fs/cgroup /dev/vda1 20M 33K 20M 1% / tmpfs 2.0M 5 2.0M 1% /run/user/0
Disk Space for a Given Filesystem
The following command shows you the usage for a given directory and the available space and percentage usage on the filesystem this directory is mounted on:
[root@centos ~]# pwd /root [root@centos ~]# df /root Filesystem 1K-blocks Used Available Use% Mounted on /dev/vda1 330147436 3237576 310116312 2% /
Check Disk Usage in Linux using du (disk usage) Command
While the df command helps you get a summary of available and occupied disk space for all the file systems on your Linux system, the du command concentrates only on occupied space. The du command stands for disk usage. A basic example of the usage of the du command is shown below:
[root@centos ~]# du 8./.ssh 1724456.
Disk Usage in Human Readable Format
Similar to df -h, here’s the du -h command:
[root@centos ~]# du -h 8.0K./.ssh 1.7G.
Disk Usage for Current Working Directory along with the Total Usage
The -c option adds a total at the end of the usage summary as shown in the result below:
[root@centos ~]# du -ch 8.0K./.ssh 1.7G. 1.7Gtotal
Disk Usage for All Files, Directories, and Subdirectories
This command will fetch the file and directory sizes of all the files, directories, and subdirectories. Be careful while using this on systems that have a very large number of files. This command is useful but when used in combination with grep or sort.
[root@centos ~]# du -ach 4.0K./.tcshrc 4.0K./.ssh/authorized_keys 8.0K./.ssh 4.0K./robots.txt 4.0K./do-bots.txt 4.0K./anaconda-ks.cfg 4.0K./cookies.txt 2.0M./Spotify201812.zip 4.0K./.bash_history 1.7G./Chicago.csv 4.0K./.bashrc 4.0K./.bash_logout 4.0K./combian.rar 4.0K./.cshrc 4.0K./.bash_profile 1.7G. 1.7Gtotal
Disk Usage after Excluding Certain Types of Files
If you want to exclude hidden files, add the –exclude “./.*” option to the du command:
[root@centos ~]# du -ach --exclude "./.*" 4.0K./robots.txt 4.0K./do-bots.txt 4.0K./anaconda-ks.cfg 4.0K./cookies.txt 2.0M./Spotify201812.zip 1.7G./Chicago.csv 4.0K./combian.rar 1.7G. 1.7Gtotal
Disk Usage with the Largest File or Directory First
One of the main reasons for checking disk usage is to clear up space occupied by redundant or unnecessary files. You can run the following command to check disk space based on the largest files first:
[root@centos ~]# du -ah --exclude "./.*" | sort -n 1.7G. 1.7G./Chicago.csv 2.0M./Spotify201812.zip 4.0K./anaconda-ks.cfg 4.0K./combian.rar 4.0K./cookies.txt 4.0K./do-bots.txt 4.0K./robots.txt
These are some of the most commonly used commands for checking disk usage and disk space in Linux.