I would like to use lvremove to delete multiple volumes by matching a regex.
For example, I have a volume group with volumes:
lv-1-8a
lv-1-846a
lv-1-5a
...
I have tried:
lvremove volgroup/lv-1-*
but it seems that this is not accepted, I get the error:
One or more specified logical volumes(s) not found.
I am able to delete the volumes one by one however,
lvremove volgroup/lv-1-846a
Logical volume "lv-1-846a" successfully removed
A Google search has not throw up any relevant results, is it possible to do this using a regex? there are a lot of volumes.
I guess you get an error message when you type
lvremove volgroup/lv-1-*
because the shell tries to expand the name volgroup/lv-1-*
inside you current directory, i.e., if you run that command from /tmp
(for instance) the shell will try to find existing files in subdirectory /tmp/volgroup
whose names start with lv-1-
. As shell finds no such file then it launches the lvremove
command with the argument volgroup/lv-1-*
... and I guess none of your LVs is named lv-1-*
.
Keep in mind that the file name expansion is carried out by the shell, not by the command (lvremove in your case). Asterisk metacharacter has no meaning for the most of commands (like lvremove) and handles as any other character.
The following examples will do what you are trying:
lvremove /dev/volgroup/lv-1-*
or
cd /etc
lvremove volgroup/lv-1-*
In both cases, shell will expand to all matching file names --- From your example above, after the shell expansions my suggested command lines really will be run as:
lvremove /dev/volgroup/lv-1-8a /dev/volgroup/lv-1-846a /dev/volgroup/lv-1-5a
or
cd /etc
lvremove volgroup/lv-1-8a volgroup/lv-1-846a volgroup/lv-1-5a
When the number of arguments is huge, the limit of the command line size may be reached. In that case the find
command is useful:
find /dev/volgroup -type l -exec lvremove -f '{}' ';'
Best regards.