0

Given a device file (ex: /dev/sda) how can I get a list of all relevant partitions?

Expected input:

/dev/sda

Expected output:

/dev/sda1
/dev/sda2
/dev/sda3
...

I know there are many ways to get information about block devices, such as lsblk, blockdev, fdisk, and blkid to name a few, but I want a solution that is the most succinct (ie, doesn't use an enormous number of pipes to grep, sed, awk, uniq etc).

What is the most condensed one liner to solve this problem?

Notes: I'm using linux (specifically ubuntu) and using either terminal or shell script to solve this problem.

3

With ls:

$ ls -1 /dev/sda?*
/dev/sda1
/dev/sda2
/dev/sda3

Or, with lsblk (which is LVM-aware):

$ lsblk -lpo NAME /dev/sda
NAME
/dev/sda
/dev/sda1
/dev/sda2
/dev/sda3
/dev/mapper/home-root
/dev/mapper/home-swap_1
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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