10

This question already has an answer here:

How to get a list of all disks, like this?

/dev/sda
/dev/sdb

marked as duplicate by don_crissti, Jenny D, G-Man, Stephen Rauch, Anthony Geoghegan Dec 24 '17 at 19:25

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

13

ls (shows individual partitions though)

# ls /dev/sd*
/dev/sda  /dev/sda1

ls (just disks, ignore partitions)

# ls /dev/sd*[a-z]
/dev/sda

fdisk

# fdisk -l 2>/dev/null |awk '/^Disk \//{print substr($2,0,length($2)-1)}'
/dev/xvda
  • simple and easy ls /dev/sd[a-z] :) – clarkk Apr 22 '17 at 11:04
  • 4
    @clarkk That wont work, once you pass "z" blockdevices; it then continues with /dev/sdaa – Alex Stragies Apr 22 '17 at 11:14
8

You can use df to list all mounted partitions, the command will output something like

$ df
/dev/sda1      230467740  37314652 181422912  18% /
udev               10240         0     10240   0% /dev
tmpfs            5599420         0   5599420   0% /sys/fs/cgroup
/dev/sdb1      961303548 130106540 782342500  15% /media/Data
..

If you want to list only your mounted disks, you can filter the output with grep, for example

$ df | grep '^/dev'
/dev/sda1      230467740  37314752 181422812  18% /
/dev/sdb1      961303548 130106540 782342500  15% /media/Data

which matches lines starting with /dev, or, if you want only the names

df | grep -o '^/dev[^ ]*' 
/dev/sda1
/dev/sdb1

which will match strings starting with /dev up to the first white space character, and output only the match (option -o, --only-matching),

Edit

The above will list all partitions, if you need to list the disks, use lsblk instead, with the following options (pattern matching as above)

$ lsblk -dp | grep -o '^/dev[^ ]*'
/dev/sda
/dev/sdb
  • -d lists only the device, without partitions
  • -p outputs the whole path

Edit 2

As pointed out below, lsblk -dpno name will give the same result, no need for grep.

  • 1
    Thanks @don_crissti, edited my answer. I should definitely go through all options when skimming over man pages. – resc Apr 22 '17 at 11:20

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