Linux List Partitions with fdisk

There are several commands that may be used to list the partitions of a Linux computer. Likely the most common one is the fdisk command. While it fdisk command is capable of just about anything, I am not always in the need to see all the information that listing the partitions provides, particularly when there are many partitions. So, I have a couple of command line arguments that I use to simplify the display a little bit.

[root@server1 ~]# fdisk -l
Disk /dev/sda: 12.9 GB, 12884901888 bytes
255 heads, 63 sectors/track, 1566 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000e8571

Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64        1084     8192000   83  Linux
/dev/sda3            1084        1212     1024000   82  Linux swap / Solaris
/dev/sda4            1212        1567     2853888    5  Extended
/dev/sda5            1212        1339     1024000   83  Linux

Disk /dev/sdb: 21.5 GB, 21474836480 bytes
213 heads, 34 sectors/track, 5791 cylinders
Units = cylinders of 7242 * 512 = 3707904 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x9088bceb

Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        5792    20970496   83  Linux

Disk /dev/sdc: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Device Boot      Start         End      Blocks   Id  System
/dev/sdc1               1         262     2104483+  82  Linux swap / Solaris
/dev/sdc2             263         275      102400   82  Linux swap / Solaris
Partition 2 does not end on cylinder boundary.

Disk /dev/sdd: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/sde: 2147.5 GB, 2147483648000 bytes
255 heads, 63 sectors/track, 261083 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000f30a1

Device Boot      Start         End      Blocks   Id  System
/dev/sde1               1      261083  2097149166   fd  Linux raid autodetect

Disk /dev/sdf: 2147.5 GB, 2147483648000 bytes
255 heads, 63 sectors/track, 261083 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0008dc93

Device Boot      Start         End      Blocks   Id  System
/dev/sdf1               1      261083  2097149166   fd  Linux raid autodetect

Disk /dev/sdg: 2147.5 GB, 2147483648000 bytes
255 heads, 63 sectors/track, 261083 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0008de46

Device Boot      Start         End      Blocks   Id  System
/dev/sdg1               1      261083  2097149166   fd  Linux raid autodetect

Disk /dev/md0: 4294.7 GB, 4294692569088 bytes
2 heads, 4 sectors/track, 1048508928 cylinders
Units = cylinders of 8 * 512 = 4096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 524288 bytes / 1048576 bytes
Disk identifier: 0x00000000

That’s a lot of information, where I only wanted to list the partitions. So, I issued the same command, this time piping it through grep looking for any line that begins (hence the “^”) with the forward slash (“/”).

[root@server1 ~]# fdisk -l | grep '^/'
/dev/sda1   *           1          64      512000   83  Linux
/dev/sda2              64        1084     8192000   83  Linux
/dev/sda3            1084        1212     1024000   82  Linux swap / Solaris
/dev/sda4            1212        1567     2853888    5  Extended
/dev/sda5            1212        1339     1024000   83  Linux
/dev/sdb1               1        5792    20970496   83  Linux
/dev/sdc1               1         262     2104483+  82  Linux swap / Solaris
/dev/sdc2             263         275      102400   82  Linux swap / Solaris
/dev/sde1               1      261083  2097149166   fd  Linux raid autodetect
/dev/sdf1               1      261083  2097149166   fd  Linux raid autodetect
/dev/sdg1               1      261083  2097149166   fd  Linux raid autodetect

What if I only wanted the Disks.

[root@server1 ~]# fdisk -l | grep 'Disk /'
Disk /dev/sda: 12.9 GB, 12884901888 bytes
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
Disk /dev/sdc: 21.5 GB, 21474836480 bytes
Disk /dev/sdd: 21.5 GB, 21474836480 bytes
Disk /dev/sde: 2147.5 GB, 2147483648000 bytes
Disk /dev/sdf: 2147.5 GB, 2147483648000 bytes
Disk /dev/sdg: 2147.5 GB, 2147483648000 bytes
Disk /dev/md0: 4294.7 GB, 4294692569088 bytes

Lets display both the disks and partitions together.

[root@server1 ~]# fdisk -l | grep 'Disk /\|^/'
Disk /dev/sda: 12.9 GB, 12884901888 bytes
/dev/sda1   *           1          64      512000   83  Linux
/dev/sda2              64        1084     8192000   83  Linux
/dev/sda3            1084        1212     1024000   82  Linux swap / Solaris
/dev/sda4            1212        1567     2853888    5  Extended
/dev/sda5            1212        1339     1024000   83  Linux
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
/dev/sdb1               1        5792    20970496   83  Linux
Disk /dev/sdc: 21.5 GB, 21474836480 bytes
/dev/sdc1               1         262     2104483+  82  Linux swap / Solaris
/dev/sdc2             263         275      102400   82  Linux swap / Solaris
Disk /dev/sdd: 21.5 GB, 21474836480 bytes
Disk /dev/sde: 2147.5 GB, 2147483648000 bytes
/dev/sde1               1      261083  2097149166   fd  Linux raid autodetect
Disk /dev/sdf: 2147.5 GB, 2147483648000 bytes
/dev/sdf1               1      261083  2097149166   fd  Linux raid autodetect
Disk /dev/sdg: 2147.5 GB, 2147483648000 bytes
/dev/sdg1               1      261083  2097149166   fd  Linux raid autodetect
Disk /dev/md0: 4294.7 GB, 4294692569088 bytes

To make this an alias command so that you don’t have to remember the entire line each time.

[root@server1 /]# alias disks="fdisk -l | grep 'Disk /\|^/'"

So next time, you can simply type disks for the same result as fdisk -l | grep ‘Disk /\|^/’. Of course you can alias to another name more suitable to your needs. To remove the alias.

[root@server1 /]# unalias disks

The goal of this exercise was to simplify the display of the fdisk output. Before anyone gets too excited about the drive sizes and the multiple swap partitions. This is a test virtual machine to test various fdisk and parted commands. Where it should be noted that parted was used to create the 4.3TB partition as fdisk is limited to 2TB partitions. Don’t use fdisk on that partition or you will risk losing all your data on that partition.