When running the standard fdisk -l
command, partition sizes are displayed in sectors by default. For example:
Disk /dev/sda: 238.5 GiB, 256060514304 bytes, 500118192 sectors
Device Start End Sectors Size Type
/dev/sda1 2048 1050623 1048576 512M EFI System
/dev/sda2 1050624 500117503 499066880 238G Linux filesystem
To make fdisk show sizes in more readable units (MB/GB), you have several approaches:
1. Using the -B flag
The most straightforward method is to use the -B
(block size) option:
fdisk -l -B=1M # Shows sizes in megabytes
fdisk -l -B=1G # Shows sizes in gigabytes
2. Alternative with blockdev
For more consistent output formatting, combine with blockdev:
blockdev --getsz /dev/sda | awk '{print $1/2048}' # Size in MB
blockdev --getsz /dev/sda | awk '{print $1/2097152}' # Size in GB
3. Parsing fdisk Output
For scripting purposes, you can parse the default output:
fdisk -l /dev/sda | awk '/^\/dev/ {print $1,$2/2048"MB"}' # Convert sectors to MB
Here are some common use cases with human-readable output:
# List all partitions with sizes in GB
fdisk -l -B=1G
# Show specific disk info in MB
fdisk -l /dev/nvme0n1 -B=1M
# Script to show all disks with GB sizes
for disk in $(lsblk -d -o NAME -n); do
echo "===== $disk ====="
fdisk -l /dev/$disk -B=1G
done
- The -B option requires fdisk from util-linux 2.23 or later
- Older systems might need to use alternative methods
- For scripting, always verify the fdisk version first
When working with disk partitions in Linux, the fdisk -l
command is a fundamental tool. By default, it displays partition sizes in sectors, which isn't always the most intuitive format for quick analysis:
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Units: sectors of 1 * 512 = 512 bytes
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 1050623 1048576 512M 83 Linux
/dev/sda2 1050624 41943039 40892416 19.5G 8e Linux LVM
For better readability, you can use these alternative approaches:
Method 1: Using --bytes, --sectors, or --unit
The modern version of fdisk supports explicit unit specification:
sudo fdisk -l --units=cylinders
sudo fdisk -l --units=sectors
sudo fdisk -l --units=bytes
For megabytes and gigabytes, combine with other tools:
sudo fdisk -l | awk '/^Disk \/dev/ {size=$5/1024/1024; print $1,$2,$3,size"MB"}'
Method 2: Using lsblk for Better Readability
For most use cases, lsblk
provides more human-friendly output by default:
lsblk -o NAME,SIZE,FSTYPE,MOUNTPOINT
Example output:
NAME SIZE FSTYPE MOUNTPOINT
sda 20G
├─sda1 512M ext4 /boot
└─sda2 19.5G LVM /
For scripting purposes, you can create a custom parser:
sudo fdisk -l | awk '
BEGIN {
units["bytes"] = 1;
units["sectors"] = 512;
units["kB"] = 1024;
units["MB"] = 1024*1024;
units["GB"] = 1024*1024*1024;
}
/^Units:/ {unit_size=$5}
/^\/dev/ {
size=$5*unit_size/units["MB"];
printf "%s: %.1fMB\n", $1, size
}'
To make this persistent, add an alias to your ~/.bashrc
:
alias fdiskh="fdisk -l | awk '/^Disk \/dev/ {size=\$5/1024/1024; print \$1,\$2,\$3,size\"MB\"}'"