52

I have a need to find all of the writable storage devices attached to a given machine, whether or not they are mounted.

The dopey way to do this would be to try every entry in /dev that corresponds to a writable devices (hd* and sd*).

Is there a better solution, or should I stick with this one?

5
  • reasking an old SO question
    – warren
    Oct 3, 2012 at 16:45
  • 2
    Perhaps you want to use udisks?
    – derobert
    Oct 3, 2012 at 17:03
  • I do not merely want to find what hard disks are on a system - I am looking for all storage devices
    – warren
    Oct 3, 2012 at 18:52
  • I think we all assumed hard disks and similar. Do you actually mean something else? Like, should it include tape drives, printers, etc.?
    – derobert
    Oct 3, 2012 at 19:22
  • 2
    @derobert - yes, hence the title of "all storage devices" :)
    – warren
    Oct 3, 2012 at 19:50

13 Answers 13

73

If one is interested only in block storage devices, one can use lsblk from widely-available util-linux package:

$ lsblk -o KNAME,TYPE,SIZE,MODEL
KNAME TYPE   SIZE MODEL
sda   disk 149.1G TOSHIBA MK1637GS
sda1  part  23.3G 
sda2  part    28G 
sda3  part  93.6G 
sda4  part   4.3G 
sr0   rom   1024M CD/DVDW TS-L632M

It lends itself well to scripting with many other columns available.

2
  • This is NOT block storage "devices", it is only list of partitions. And you can't use this command to get devices which does not have any partition or not formatted. Oct 28, 2020 at 12:27
  • @YoushaAleayoub sda and sr0 are it (devices). lsblk lists only block devices if you run it with --nodeps program switch.
    – K3---rnc
    Oct 28, 2020 at 18:38
18

You could trawl through the output of lshw and extract details about devices in the disk or tape class (and maybe others - storage class gives you details on storage controllers, scsi, sata, sas, etc).

e.g.

lshw -class disk -class tape

The -short option gives a nice compact summary. e.g. on my home zfsonlinux server/workstation/experiment-box (no tape devices unfortunately):

# lshw -class tape -class disk -class storage -short
H/W path              Device      Class       Description
=========================================================
/0/100/4/0                        storage     JMB362 SATA Controller
/0/100/5/0            scsi10      storage     JMB362 SATA Controller
/0/100/5/0/0.0.0      /dev/sdc    disk        120GB Patriot Wildfire
/0/100/b/0            scsi1       storage     SAS2008 PCI-Express Fusion-MPT SAS-2 [Falcon]
/0/100/b/0/0.0.0      /dev/sdd    disk        1TB WDC WD10EARS-00Y
/0/100/b/0/0.1.0      /dev/sde    disk        1TB WDC WD10EACS-00Z
/0/100/b/0/0.2.0      /dev/sdf    disk        1TB WDC WD10EACS-00Z
/0/100/b/0/0.3.0      /dev/sdg    disk        1TB ST31000528AS
/0/100/b/0/0.4.0      /dev/sdh    disk        1TB ST31000528AS
/0/100/b/0/0.5.0      /dev/sdi    disk        1TB ST31000528AS
/0/100/b/0/0.6.0      /dev/sdj    disk        1TB ST31000528AS
/0/100/11             scsi2       storage     SB7x0/SB8x0/SB9x0 SATA Controller [AHCI mode]
/0/100/11/0           /dev/sda    disk        128GB Patriot Torqx 2
/0/100/11/1           /dev/sdb    disk        1TB ST31000528AS
/0/1                  scsi11      storage     
/0/1/0.0.0            /dev/sdk    disk        1967MB SCSI Disk
/0/1/0.0.1            /dev/sdl    disk        SCSI Disk
/0/1/0.0.2            /dev/sdm    disk        SCSI Disk
/0/1/0.0.3            /dev/sdn    disk        SCSI Disk
/0/2                  scsi66      storage     
/0/2/0.0.0            /dev/sdo    disk        SCSI Disk
/0/3                  scsi67      storage     
/0/3/0.0.0            /dev/sdp    disk        4057MB SCSI Disk

The /0/1 devices are actually a USB card-reader (there's a 2GB SD card plugged in to one of the slots), and the /0/2 device is my android phone plugged in for charging only. The 0/3 device is a 4GB USB flash drive.

lshw can produce plain text, html, xml, and json output. It can also dump hardware details into an sqlite database format.

It is packaged for debian and most other distros. The home page and source is at http://ezix.org/project/wiki/HardwareLiSter

1
  • This command is really impressive! Helped a ton! Sadly, it doesn't show which drives are connected through SAS expanders. Is there a way to see that as well? I know it's possible when you get the X:X:X:X syntax.
    – Sawtaytoes
    Apr 18 at 4:05
8

You can use lsblk to list all block devices, along with whether or not each device is read only.

You can then use grep and awk to print the names of block devices that are not read only:

lsblk -d -n -oNAME,RO | grep '0$' | awk {'print $1'}
2
  • this is a great expansion on the previous answer
    – warren
    Sep 19, 2014 at 23:11
  • You can use awk to do grep's work as well: awk '/0$/ {print $1}'
    – mwfearnley
    Dec 20, 2022 at 9:33
7

Please, try this command

ls -l /dev /dev/mapper |grep '^b'

It will list you all block devices in your system

4

lsblk will list all block storage devices.

fdisk -l will list all of the partitions on all devices that are listed in /proc/partitions

lshw -short will give you information about all of the hardware (except perhaps firewire) on the system.

1
  • "lsblk will list all block storage devices." NO. This lists all partitions... Oct 28, 2020 at 12:43
4

You can use hwinfo to list all disks (documentation).

hwinfo --block --short gives an overview:

disk:
  /dev/sdb             WDC WD3200AAKS-7
  /dev/sda             SAMSUNG HD103UJ
partition:
  /dev/sdb1            Partition
  /dev/sdb2            Partition
  /dev/sda1            Partition
cdrom:
  /dev/sr1             TSSTcorp DVD+-RW TS-H653B
  /dev/sr0             HL-DT-ST DVD-ROM GDRH20N

hwinfo --disk gives more details for each disk.

FYI: on some Linux distributions such as Ubuntus 14.04 and higher, hwinfo isn't present in the official repository.

3

If, as per your response to derobert, you're looking for something that lists tape drives and printers, you may be interested in lsdev, lsusb and lspci.

2
  • Of course, that'll fail to find network-attached storage :-(
    – derobert
    Oct 4, 2012 at 15:21
  • 1
    Then fdisk -l
    – colons
    Oct 4, 2012 at 21:18
2

You can try the following command :

file /dev/disk/by-id/* | awk -F'/' 'NR>1{print "\047/dev/"$NF}' | sort | uniq
1
  • 1
    what does it do and how does that compare to listing block and tape device classes with lshw?
    – n611x007
    Sep 10, 2015 at 7:46
0

I have found that fdisk and lsblk are not available in the virtual terminal for the Debian installer. In this case I use blkid, which gets the UUID for block storage devices.

0

If you mean to RAID devices then you would not be able to get correct result using hdparm smartctl etc because these kind of tools look for /dev which is OS level if you would like to know on Hardware Level then you need to have tool like megacli i used to debug with megacli

    [root@ns3539186 ~]# /opt/megaraid/megacli -LDPDInfo -aAll |grep "Virtual Disks\|RAID Level\|State"
Number of Virtual Disks: 2
RAID Level          : Primary-1, Secondary-0, RAID Level Qualifier-0
State               : Optimal
Foreign State: None
Media Type: Solid State Device
Foreign State: None
Media Type: Solid State Device
RAID Level          : Primary-1, Secondary-0, RAID Level Qualifier-0
State               : Optimal
Foreign State: None
Foreign State: None

To get serial number of the disk

[root@ns3539186 ~]# /opt/megaraid/megacli -PDList -aAll | egrep 'Slot\ Number|Device\ Id|Inquiry\ Data|Raw|Firmware\ state' | sed 's/Slot/\nSlot/g' |grep "Da                    ta\|Slot\|Raw"
Slot Number: 0
Raw Size: 447.130 GB [0x37e436b0 Sectors]
Inquiry Data: PHYS733402Z0480BGN  INTEL SSDSC2KB480G7                     SCV10100
Slot Number: 1
Raw Size: 447.130 GB [0x37e436b0 Sectors]
Inquiry Data: PHYS733402YV480BGN  INTEL SSDSC2KB480G7                     SCV10100
Slot Number: 2
Raw Size: 3.638 TB [0x1d1c0beb0 Sectors]
Inquiry Data: K3GJTYMB            HGST HUS726040ALA610                    A5GNT920
Slot Number: 3
Raw Size: 3.638 TB [0x1d1c0beb0 Sectors]
Inquiry Data: K3GHW57B            HGST HUS726040ALA610                    A5GNT920

In above output Inquiry Data contains Serial number

-1

Here are the couple of commands I used to find local and shared storage.

For local storage

lsblk | grep -v '^loop'

For shared storage

findmnt -D | grep -v '^tmpfs' | grep -v '^/'

OR

df -kh | grep -v '^tmpfs' | grep -v '^/'
2
  • Why resuggest the same tools already suggested (and accepted) 6+ years ago?
    – warren
    Nov 12, 2019 at 15:17
  • Because the OS changed and now you get a lot of loop and tmpfs partition. So its better to clean the output using grep as above.
    – Mian Ahmad
    Nov 13, 2019 at 15:03
-1

List all block devices:

find / -type b 2>/dev/null
1
  • 1
    sorry, but that also finds virtual things like docker containers in /var and items in /run
    – warren
    Jul 29, 2021 at 21:08
-4

This will list all your disks and mounted drives:

$ df -h
1
  • 5
    Actually, that lists filesystems…
    – HalosGhost
    Oct 4, 2014 at 4:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .