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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Let's assume we have lv path:


Than I split lvdisplay -c with cut:

vgname="$(lvdisplay -c "$lvpath" | cut -d ':' -f 2 )"
lvname="$(basename "$(lvdisplay -c "$lvpath" | cut -d ':' -f 1 )" )"


$ echo VG: $vgname LV: $lvname
VG: vg2 LV: Music


Is there more convenient way of doing this ?

Can I rely on lvdisplay -c that will always provide path in form /dev/VGname/LVname ? (In case of /dev/mapper/VGname-LVname basename sould be exchanged with sed)

Is there tool from LVM2 package to obtain LVname from path?

share|improve this question
There are many tools and switches for obtaining LVM info. Could you give an example of a use case for your need? Just from the path itself, you can't be certain that a lv doesn't have an embedded hyphen in it's name. – bsd Mar 24 '12 at 16:06
up vote 2 down vote accepted

Use lvs with the -o option to customize the displayed fields.

set $(lvs --noheadings -o lv_name,vg_name)
lv_name=$1 vg_name=$2

Note that you can't just write lv_name=$(lvs --noheadings -o lv_name) because lvs puts extra whitespace around the value. The snippet is safe because volume names aren't allowed to contain shell special characters.

share|improve this answer
Is lvname="$(echo `lvs --noheadings -o lv_name "$lvpath"`)" safe in your opinion ? – Grzegorz Wierzowiecki Mar 26 '12 at 14:38
@GrzegorzWierzowiecki It's safe but looks awfully complicated. – Gilles Mar 26 '12 at 16:49
I know, I just don't want to overwrite parameters $1 , $2 . On the other hand I can encapsulate your solution into function to prevent set of changing global parameters. Thanks :) – Grzegorz Wierzowiecki Mar 26 '12 at 19:46

Your Answer


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.