how to list all hard disks in linux from command line
Hard Disk, Hard Drive, Disk Drive or Hard Disk Drive are all names for a data storage device (hardware device) for storing and retrieving digital information usually in a computer. A computer can have multiple hard disks attached to it, both internal and external.
Now these hard disks can be further divided to multiple logical containers in order to host different file systems or to keep file systems/files separate. These are called partitions and they can then mounted independently with out affecting the other disks and partitions. At a high level abstraction, you can view partition as separate disks as well.
Hard disks on a system are detected and/or identified by various device drivers in the kernel and then assigned an unique device id at boot time, enabling it to be mounted and read later (yeah, this is an over simplification of how it all works but it should suffice for this post). We will see later in the post how you can list disks that have been identified by the system.
The hard disks can be differentiated based on the interface used to interact with them. Some of the commonly used types of disk are SCSI (Small Computer System Interface), ATA or IDE (Advanced Technology Attachment), SATA (Serial ATA), SAS (Serial Attached SCSI) among others. As I mentioned, the physical hard disk is assigned an unique id at startup. This can configured (using udev among others) so that you can assign it pretty much id, but usually most systems follow some universally accepted conventions when naming devices.
By convention, the IDE disks use the device id prefixed with hd and the SCSI (and SATA) disks prefix their device id with sd. So, an IDE disk would be located at /dev/hd(*). eg: /dev/hda, /dev/hdb etc. Similarly, the SCSI disks would be /dev/sda, /dev/sdb etc or in general of the format /dev/sd(*).
There are several different commands that you can use in a Linux environment to list disks that have been mounted on the system.
df
The df command is primarily intended to report file system disk space usage. It is still a good utility to print out the disks that are available to the system, although it prints filesystems rather than disks per se.
You can use the -h or –human-readable option with df to print out the disk usage in a human readable format. Look for file systems that identify as /dev/sda, /dev/sdb or /dev/hda to identify the disks.
lsblk
The lsblk command is to list block devices. It reads the data from udev database along with sysfs file system to print a report on all available or specified block devices. Block devices abstracts the actual hardware characteristics of any physical device and allows you to interact with it using a buffered interface.
The lsblk command with out any argument will print out the block devices in a tree format. Again look for names, such as sda, sdb etc. The top level denotes the disk and the first level children represent the partitions with in the block.
The are several useful command line options that can be used with lsblk command. The -d or –nodeps will print out the device and not the partitions. The -S or –scsi will output only the SCSI devices.
lshw
Another commonly used utility is lshw, which can print out detailed information about your hardware. Again it might not be default in some distros even though it is a system application.
bash$ lshw -class disk
The above command will list all disks on the system. You can also specify the storage class if you want to print out the storage controllers as well. You can further reduce the verbosity of the output by using the -short option
bash$ lshw -class disk -class storage -short
blkid
The previously mentioned lsblk command is a better and recommended option that the blkid command. I include it here just for the sake of completeness.
blkid will print out several different attributes about the block devices. You can usually make out the disks and partitions from the output just as with lsblk.
bash$ blkid
fdisk
fdisk is a popular command mostly used to manipulate the partition table. You can use it to list all partitions from the partition table and find the devices that are available. The –list or -l command line option will print out all the known partitions from all devices.
bash$ fdisk -l
You are probably looking for something like this in the output.
Disk /dev/sda: 149.1 GiB, 160041885696 bytes, 312581808 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 Disklabel type: dos Disk identifier: 0xa461a461
Disk /dev/sdb: 111.8 GiB, 120034123776 bytes, 234441648 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 Disklabel type: dos Disk identifier: 0x00000000
There are couple of other partition table manipulation tools built on top of fdisk, with more options and features. sfdisk and cfdisk are two such examples. They might not however be installed in most distros.
parted
Along the lines of fdisk, parted is another partition table manipulation utility. Again you can use the –list or -l command line option to print out the devices or disks and all its info. I like the default output of parted better than fdisk, but then that is subjective.
bash$ parted -l
Model: ATA ST3160815AS (scsi) Disk /dev/sda: 160GB Sector size (logical/physical): 512B/512B Partition Table: msdos Disk Flags:
proc file
Another rudimentary way is to print out the contents of the proc/partitions folder. This will print out all known devices and partitions in the system. By this time you should be able to differentiate between the disk and partitions, i assume.
bash$ cat /proc/partitions
Look for minor '0' or name that conforms to the /dev/sda format.
lsscsi
If you know that you have only SCSI devices or only need the information about SCSI disks, then you can use the lsscsi command. This utility might not be installed on some distros by default.
As the command name suggests, it prints out all information about the SCSI devices on the system.
bash$ lsscsi
No matter what distribution you are on, you should be able to list disks on your system using at least one of the commands listed above.