So are you wondering, How to check disk space in Linux command line? Let me help you!
In Linux, checking disk space is an essential task for system administrators to monitor the usage of available storage space. The process involves inspecting the current amount of used and free space on the hard drive, which is important to ensure that the system can continue to operate optimally. There are various methods to check disk space in Linux, and in this response, we will explore some of the most commonly used ones.
Different methods to check disk space in Linux
Here are the different methods to check disk space in Linux, explained in points:
Command Line Interface
- Use the built-in Linux utility command '
df
' - Syntax: '
df [options] [filesystem]
' - Options include '
-h
' to display output in a human-readable format and '-T
' to show file system type
Graphical User Interface
- Most Linux distributions come with a file manager that displays disk usage information
- For example, the Nautilus file manager in Ubuntu displays disk space information in the lower-left corner of the window
- Other file managers, such as Thunar in Xubuntu and Dolphin in KDE, have similar features
Third-Party Disk Space Monitoring Tools
- Install third-party tools to provide more detailed information about disk usage
- Popular tools include Baobab, DiskUsage, and Ncdu
- These tools can provide information on which directories and files are taking up the most space
Here are some of the CLI tools which you can use to check disk space in Linux
1. df
command
The df
command to check disk space in Linux is widely used by Linux Administrators. It is a built-in utility that displays the amount of free and used disk space on the file system. It is used to report the amount of available and used disk space on the file system, including file systems mounted on remote machines. It can be used to check disk space.
By default, the df
command displays the sizes of the file systems in 1-kilobyte blocks and the available and used space in the same units. The output of df
includes the file system's device name, total size, used space, available space, percentage of used space, and the mount point of the file system.
You can use df
with -Th
to print the partition type and the partition size in human readable format. This command will show you the total available, used and free space per partition.
You can also use other arguments such as -i
to print the available, used and free inode count per partition:
We can use awk to get the desired output, you can modify this based on your requirement. Currently it prints total size of the partition.
# df -h | awk '{printf "%-20s %s\n", $1, $2}' Filesystem Size devtmpfs 1.8G tmpfs 1.9G tmpfs 745M /dev/mapper/rl-root 18G /dev/sda1 1014M tmpfs 373M overlay 18G tmpfs 373M overlay 18G
You can check the man page of df command for other supported arguments to check disk space in different formats.
2. fdisk
command
fdisk
is another very handy utility for system administrators. fdisk is a user interactive program mostly used for creation and manipulation of partition tables such as creating, deleting, modifying partitions. But we can also use this utility to list the available disks connected to the Linux system along with the respective disk size.
Here is a snippet of output from command "fdisk -l
"
As you can see from the output, I have a disk /dev/sda
with a size of 15GB. The output also shows more details about this disks wherein it contains two partitions /dev/sda1
and /dev/sda2
.
The output of fdisk -l
can be quiet long so if you just wish to check the disk space then you can use this command:
~]# fdisk -l | awk '$1=="Disk" && $2 ~ /^\/dev\/.*/ {print $2 $3 $4}' /dev/sda:15GiB, /dev/sdb:8GiB, /dev/mapper/rhel-root:14GiB, /dev/mapper/rhel-swap:512MiB,
Here I have combined fdisk
with awk
to only print required sections. So I have two disks with 15GB and 8GB each, while I have two LVM partitions which are most likely part of these disks with a size of 14GB and 512MB.
3. parted
command
parted is another alternative to fdisk
and is also used for manipulating disk partitions and is available by default in many Linux distributions such as Ubuntu. It is useful for creating space for new operating systems, reorganizing disk usage, and copying data to new hard disks. But we can also use it to check disk space on Linux.
Similar to fdisk
, we can use parted -l
to list all the available disks along with their respective size:
If you compare the output of parted
with fdisk
, here we only get the available disk and their size and the LVM details are missing which is a good thing as we only wanted to check the disk space.
We can also use awk
with parted to further only print required output:
~]# parted -l | awk '$1=="Disk" && $2 ~ /^\/dev\/.*/ {print $2 $3 $4}' /dev/sda:16.1GB /dev/sdb:8590MB
4. du
command
The du
command stands for "disk usage", and it is a standard Linux command that is used to estimate the space used by a file or directory on a file system. It can be used to determine which files or directories are taking up the most disk space. du
gives you flexibility to check disk space by folder in Linux.
The basic syntax of the du
command is as follows:
du [options] [directory or file name]
By default, du
displays the disk usage of each directory and subdirectory in the current directory. If you specify a directory or file name, it will display the disk usage of that particular directory or file.
Here are some commonly used options for the du command:
-h
: Display the disk usage in a human-readable format, such as "1.2K" or "3.4M".-s
: Display only a total sum of the disk usage for the specified directory or file.-c
: Display a grand total of the disk usage for all the directories and files that are specified.-a
: Display the disk usage for all files and directories, including hidden files and directories.
Here are some example demonstrating different scenarios to check file system size using du
command:
To check the disk usage of a particular directory:
du -sh /path/to/directory
To check disk space used by a single file:
du -h file.txt
To check disk space used by a directory and all its subdirectories:
du -h directory/
To check the total disk space used by a directory and all its subdirectories:
du -h -c directory/
To display the sizes of all files and directories in a directory (not just directories):
du -h -a directory/
To check size of all the files under a partition recursively
To get total summary of size of files in a partition or directory
We can use du -c
to print a total or summary of all the file size under provided directory or partition.
~]# du -sch /etc/iscsi/*
4.0K /etc/iscsi/initiatorname.iscsi
16K /etc/iscsi/iscsid.conf
20K total
To sort the output based on file size
By default the du command will print the size of the file based on the first available directory or sub-directory. So the output is not sorted and it can be a tedious task to go through the long list. We can combine du
with sort
command do sort the output based on the file size.
Here we have used du
with sort -hr
where -h
is used to compare human readable numbers such as 2K, 1G, 4M etc while -r
is used to reverse the order of search.
To print file size larger than specified size
By default du will print the size of every file found under a partition or directory. We can add a threshold to print files higher than a certain size.
For example here we are printing the files with size higher than 1MB under /var/log
~]# du -ach -t 1M /var/log/* | sort -hr
19M total
8.9M /var/log/anaconda
4.2M /var/log/anaconda/lvm.log
3.2M /var/log/anaconda/journal.log
1.9M /var/log/dnf.librepo.log
1.6M /var/log/audit/audit.log
1.6M /var/log/audit
For more list of supported options check the man page of du command.
5. lsblk
command
lsblk
lists information about all available or the specified block devices. The lsblk
command reads the sysfs filesystem and udev db to gather information. If the udev db is not available or lsblk is compiled without udev support than it tries to read LABELs, UUIDs and filesystem types from the block device.
We can use lsblk
with -o LIST
or --output LIST
to get desired columns based on the value of LIST. Use lsblk --help
to get a list of all supported columns.
We will use name, fstype, size, mountpoint LIST for our example. Although using only size and name was enough but to give more detailed output I am using additional list options.
~]# lsblk -o NAME,SIZE,FSTYPE NAME SIZE FSTYPE sda 20G ├─sda1 1G xfs └─sda2 18.5G LVM2_member ├─rl-root 17.5G xfs └─rl-swap 1G swap sdb 2G ├─sdb1 2G zfs_member └─sdb9 8M sdc 2G ├─sdc1 2G zfs_member └─sdc9 8M sr0 58.4M iso9660
6. blockdev
command
We can use blockdev
command to print a report for the specified device. It is possible to give multiple devices. If none is given, all devices which appear in /proc/partitions
are shown. Note that the partition StartSec
is in 512-byte sectors.
You can also use following commands:
# returns size in bytes. # blockdev --getsize64 /dev/sda # returns size in 512-byte sectors. # blockdev --getsz /dev/sda
7. lshw
command
lshw is a small tool to extract detailed information on the hardware configuration of the machine. It can report exact memory configuration, firmware version, mainboard configuration, CPU version and speed, cache configuration, bus speed, etc. on DMI-capable x86 or IA-64 systems and on some PowerPC machines.
By default lshw will give you a huge output, we can limit that by only printing the information for a specified class such as "disk
". To get the list of attached disks and their details such as size we will use lshw -c disk
which can also be used to check disk space in Linux.
Here is an output snippet from my Linux node:
We can further improve the output using lshw
with grep
:
~]# lshw -c disk | grep -E "logical name|size:" logical name: /dev/sda size: 15GiB (16GB) logical name: /dev/sdb size: 8GiB (8589MB)
8. Checking disk size from the system logs
We can also use system logs such as boot logs using dmesg
or journalctl -b
and try to search for respective disk to get more information. The catch is that you should know the disk name unlike other methods which we discussed where we were able to check size of all the available disks without prior knowledge of disk name.
For example, here I am searching for all instance of sda
disk in boot logs using dmesg
:
Similarly we can search for any other disk, such as sdb
:
~]# dmesg | grep sdb [ 3.230296] sd 3:0:0:0: [sdb] 16777216 512-byte logical blocks: (8.59 GB/8.00 GiB) [ 3.230304] sd 3:0:0:0: [sdb] Write Protect is off [ 3.230306] sd 3:0:0:0: [sdb] Mode Sense: 00 3a 00 00 [ 3.230316] sd 3:0:0:0: [sdb] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA [ 3.238804] sdb: sdb1 [ 3.239698] sd 3:0:0:0: [sdb] Attached SCSI disk
You may also try grepping blocks
in dmesg
output which can list the available disk with their size:
~]# dmesg | grep blocks [ 3.228462] sd 2:0:0:0: [sda] 31457280 512-byte logical blocks: (16.1 GB/15.0 GiB) [ 3.230296] sd 3:0:0:0: [sdb] 16777216 512-byte logical blocks: (8.59 GB/8.00 GiB)
9. lsscsi
command
lsscsi
uses information in sysfs (Linux kernel series 2.6 and later) to list SCSI devices (or hosts) currently attached to the system. Many non-SCSI storage devices (but not all) used the SCSI subsystem in Linux. In lsscsi version 0.30 support was added to list NVMe devices.
We can use lsscsi --size
to list all the connected storage devices along with their size as shown below:
~]# lsscsi --size [0:0:0:0] cd/dvd VBOX CD-ROM 1.0 /dev/sr0 - [1:0:0:0] cd/dvd VBOX CD-ROM 1.0 /dev/sr1 - [2:0:0:0] disk ATA VBOX HARDDISK 1.0 /dev/sda 16.1GB [3:0:0:0] disk ATA VBOX HARDDISK 1.0 /dev/sdb 8.58GB
10. sfdisk
command
sfdisk
is again an alternative to fdisk
and parted
utility. One of the major difference between fdisk
and sfdisk
is that sfdisk
reads and writes partition tables, but is not interactive like fdisk
or cfdisk
(it reads input from a file or stdin). It's generally used for partitioning drives from scripts or for partition table backup and recovery.
But it can also be used to check the disk size using sfdisk -l
, sample output:
Summary
In Linux, there are several ways to check the available disk space in the system. One way is to use the "df
" command, which shows the disk space usage of file systems on the system. Running "df -h
" in the terminal will display the total hard disk space available, along with the available and used disk space for each file system.
To check disk space usage by folder in Ubuntu, the "du
" command can be used. By default, "du
" displays the disk space used by each directory and its subdirectories. Running "du -sh /path/to/folder
" will display the total size of the folder in a human-readable format.
In order to check the disk space in a terminal, you can use any of these commands mentioned above, as all of them display the available disk space. Using the "df
" command will show the total space available for all file systems mounted on the system, whereas the "du
" command can be used to see the disk space usage of a particular folder.
Overall, monitoring disk space usage is important to ensure that you have sufficient storage available for your system and applications to operate efficiently. By using commands like "df
" and "du
", you can easily check the disk space usage of your system and make informed decisions about managing your storage resources.