Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
    
reasking an old SO question –  warren Oct 3 '12 at 16:45
    
Perhaps you want to use udisks? –  derobert Oct 3 '12 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 '12 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 '12 at 19:22
    
@derobert - yes, hence the title of "all storage devices" :) –  warren Oct 3 '12 at 19:50

5 Answers 5

If one is interested only in block storage devices, one can use lsblk from 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

Many other columns available. ;-)

share|improve this answer

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

share|improve this answer

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.

share|improve this answer
    
Of course, that'll fail to find network-attached storage :-( –  derobert Oct 4 '12 at 15:21
    
Then fdisk -l –  Iain Dawson Oct 4 '12 at 21:18

Please, try this command

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

It will list you all block devices in your system

share|improve this answer

You can try the following command :

file /dev/disk/by-id/* | awk -F'/' 'NR>1{print "\047/dev/"$NF}' | sort | uniq
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.