Get simple list of all disks
Solution 1
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
Solution 2
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
.
Related videos on Youtube
Anthony Chang
https://dynaccount.dk https://dynaccount.dk/bogfoeringsprogram/ https://dynaccount.dk/regnskabsprogram/
Updated on September 18, 2022Comments
-
Anthony Chang almost 2 years
How to get a list of all disks, like this?
/dev/sda /dev/sdb
-
Alex Stragies about 7 years@clarkk That wont work, once you pass "z" blockdevices; it then continues with /dev/sdaa
-
resc about 7 yearsThanks @don_crissti, edited my answer. I should definitely go through all options when skimming over man pages.