How to get the number of physical disks in Linux?

sudo fdisk -l will list your disks and a bunch of stats about them, including the partitions. The disks are generally in the form of /dev/sdx and partitions /dev/sdxn, where x is a letter and n is a number (so sda is the first physical disk and sda1 is the first partition on that disk).

sudo df -h gives you the size and usage stats per partition. Drop the -h and you get usage in blocks, with it it's human readable.

I put the sudos in there because I got no output from fdisk and only partial output from df when I ran the commands as a regular user, I suppose because the commands read from somewhere off limits to non-admins.


If you really want to display only the hardware, and not RAID volumes and partitions that might be seen by the OS as physical drives. You might want to try lshw

lshw -class disk -short
H/W path        Device      Class       Description
===================================================
/0/1/0.0.0      /dev/cdrom  disk        DVD-RAM GSA-H55N
/0/1/0.1.0      /dev/sda    disk        160GB ST3160021A
/0/2/0.0.0      /dev/sdb    disk        160GB ST3160815AS

Or a bit much verbose

lshw -class disk
  *-cdrom                 
   description: DVD-RAM writer
   product: DVD-RAM GSA-H55N
   vendor: HL-DT-ST
   physical id: 0.0.0
   bus info: scsi@0:0.0.0
   logical name: /dev/cdrom
   logical name: /dev/sr0
   version: 1.04
   serial: [
   capabilities: removable audio cd-r cd-rw dvd dvd-r dvd-ram
   configuration: ansiversion=5 status=nodisc
  *-disk
   description: ATA Disk
   product: ST3160021A
   vendor: Seagate
   physical id: 0.1.0
   bus info: scsi@0:0.1.0
   logical name: /dev/sda
   version: 8.01
   serial: 5JS97CFY
   size: 149GiB (160GB)
   capabilities: partitioned partitioned:dos
   configuration: ansiversion=5 sectorsize=512 signature=000f3a2f
  *-disk
   description: ATA Disk
   product: ST3160815AS
   vendor: Seagate
   physical id: 0.0.0
   bus info: scsi@2:0.0.0
   logical name: /dev/sdb
   version: 3.AA
   serial: 9RX7AK36
   size: 149GiB (160GB)
   capabilities: partitioned partitioned:dos
   configuration: ansiversion=5 sectorsize=512 signature=000b6d91

I think the easiest way (at least concerning parsing effort) on a recent Linux installation would be

$ lsblk -S

which outputs something like this:

tremendous:~# lsblk -S
NAME HCTL       TYPE VENDOR   MODEL             REV TRAN
sda  0:0:0:0    disk ATA      WDC WD5000AUDX-6 01.0 sata
sdb  1:0:0:0    disk ATA      WDC WD5000AUDX-6 01.0 sata
tremendous:~# 

Tags:

Linux

Unix