How to Get a List of All Storage Devices Connected to a Linux Machine

Nilesh Katuwal Feb 02, 2024
  1. Use the /Proc/Partitions Directory to Get Information About the Associated Storage Devices in Linux
  2. Use the fdisk -l Command to Work With Disks and Partitions in Linux
  3. Use the lsblk Command to Print the Partition Hierarchy in Linux
  4. Conclusion
How to Get a List of All Storage Devices Connected to a Linux Machine

We frequently need to inspect the storage devices on a machine.

This is especially handy when we need to verify that all hard disks and SSDs are detected on the system and that any external storage devices are handled correctly.

Linux provides several options for displaying the storage devices linked to the system. We’ll go through it individually in this tutorial.

Use the /Proc/Partitions Directory to Get Information About the Associated Storage Devices in Linux

Every Linux distribution includes a /proc directory, which contains various files that provide various types of information on the system’s present state.

This is a virtual file system. It means that these files don’t exist on the disk, but their paths can be read as genuine files by any application or command.

The file /proc/partitions contains information about the associated storage devices. So, if we execute the cat command on the /proc/partitions directory, we’ll get the following information:

$ cat /proc/partitions

Output:

major     minor  #blocks  name

   7        0          4 loop0
   7        1      56840 loop1
   7        2     168712 loop2
   7        3      63392 loop3
   7        4     224256 loop4
   7        5      66660 loop5
   7        6       6024 loop6
   7        7     224256 loop7
   8        0  250059096 sda
   8        1     524288 sda1
   8        2          1 sda2
   8        5  249532416 sda5
  11        0    1048575 sr0
   7        8      33072 loop8
   7        9     302812 loop9
   7       10     355308 loop10
   7       11      66776 loop11
   7       12     541348 loop12
   7       13      55540 loop13
   7       14      44308 loop14
   7       15     253856 loop15
   7       16      52180 loop16
   7       17     330648 loop17
   7       18     355364 loop18
   7       19      56820 loop19
   7       20     159352 loop20
   7       21      18404 loop21

On the other hand, this approach displays the output in blocks with the labels of each partition.

Use the fdisk -l Command to Work With Disks and Partitions in Linux

The fdisk is a Linux command that allows you to work with disks and partitions. This may work unless you run it as root or with sudo:

$ sudo fdisk -l

Output:

Disk /dev/loop0: 4 KiB, 4096 bytes, 8 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/loop1: 55.52 MiB, 58204160 bytes, 113680 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/loop2: 164.78 MiB, 172761088 bytes, 337424 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/loop3: 61.93 MiB, 64913408 bytes, 126784 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/loop4: 219 MiB, 229638144 bytes, 448512 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/loop5: 65.1 MiB, 68259840 bytes, 133320 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/loop6: 5.9 MiB, 6168576 bytes, 12048 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/loop7: 219 MiB, 229638144 bytes, 448512 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/sda: 238.49 GiB, 256060514304 bytes, 500118192 sectors
Disk model: Netac SSD 256GB 
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x5c281f6c

Device     Boot   Start       End   Sectors  Size Id Type
/dev/sda1  *       2048   1050623   1048576  512M  b W95 FAT32
/dev/sda2       1052670 500117503 499064834  238G  5 Extended
/dev/sda5       1052672 500117503 499064832  238G 83 Linux

Disk /dev/loop8: 32.3 MiB, 33865728 bytes, 66144 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/loop9: 295.73 MiB, 310079488 bytes, 605624 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/loop10: 346.10 MiB, 363835392 bytes, 710616 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/loop11: 65.22 MiB, 68378624 bytes, 133552 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/loop12: 528.68 MiB, 554340352 bytes, 1082696 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/loop13: 54.24 MiB, 56872960 bytes, 111080 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/loop14: 43.28 MiB, 45371392 bytes, 88616 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/loop15: 247.93 MiB, 259948544 bytes, 507712 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/loop16: 50.98 MiB, 53432320 bytes, 104360 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/loop17: 322.92 MiB, 338583552 bytes, 661296 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/loop18: 347.4 MiB, 363892736 bytes, 710728 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/loop19: 55.5 MiB, 58183680 bytes, 113640 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/loop20: 155.63 MiB, 163176448 bytes, 318704 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/loop21: 17.10 MiB, 18845696 bytes, 36808 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

The result, as seen above, is extremely detailed and well-formatted. It lists all of the storage devices connected to the system and their total size, model, label, partitions, and other pertinent information.

Use the lsblk Command to Print the Partition Hierarchy in Linux

$ lsblk

Output:

NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
loop0    7:0    0     4K  1 loop /snap/bare/5
loop1    7:1    0  55.5M  1 loop /snap/core18/2284
loop2    7:2    0 164.8M  1 loop /snap/gnome-3-28-1804/161
loop3    7:3    0  61.9M  1 loop /snap/core20/1270
loop4    7:4    0   219M  1 loop /snap/gnome-3-34-1804/77
loop5    7:5    0  65.1M  1 loop /snap/gtk-common-themes/1515
loop6    7:6    0   5.9M  1 loop /snap/notepad-plus-plus/335
loop7    7:7    0   219M  1 loop /snap/gnome-3-34-1804/72
loop8    7:8    0  32.3M  1 loop /snap/snapd/12704
loop9    7:9    0 295.7M  1 loop /snap/vlc/2344
loop10   7:10   0   347M  1 loop /snap/wine-platform-runtime/279
loop11   7:11   0  65.2M  1 loop /snap/gtk-common-themes/1519
loop12   7:12   0 528.7M  1 loop /snap/pycharm-community/265
loop13   7:13   0  54.2M  1 loop /snap/snap-store/558
loop14   7:14   0  43.3M  1 loop /snap/snapd/14295
loop15   7:15   0 247.9M  1 loop /snap/gnome-3-38-2004/87
loop16   7:16   0    51M  1 loop /snap/snap-store/547
loop17   7:17   0 322.9M  1 loop /snap/wine-platform-6-stable/14
loop18   7:18   0   347M  1 loop /snap/wine-platform-runtime/280
loop19   7:19   0  55.5M  1 loop /snap/core18/2253
loop20   7:20   0 155.6M  1 loop /snap/opera/158
loop21   7:21   0    18M  1 loop /snap/chromium-ffmpeg/24
sda      8:0    0 238.5G  0 disk 
├─sda1   8:1    0   512M  0 part /boot/efi
├─sda2   8:2    0     1K  0 part 
└─sda5   8:5    0   238G  0 part /
sr0     11:0    1  1024M  0 rom  

The partition hierarchy is printed, and we can see which disks are attached and which partitions are present beneath them, as shown above. However, just the device labels, not the device names, are printed.

Conclusion

This article looked at three ways to list the storage devices attached to a Linux system, with fdisk and parted producing fairly similar detailed data.

The results of cat /proc/partitions and lsblk are fairly brief, and we might utilize them in a bash script for further processing.