#!/bin/sh # # removes all libvirt domains older than 1 day matching given regex VM_REGEX='$test_' remove_domain () { echo "Destroying and undefining $vm" virsh destroy "$1" virsh undefine --remove-all-storage --snapshots-metadata "$1" } for vm in $(find /etc/libvirt/qemu -maxdepth 1 -type f -mmin +1440 -exec basename -s .xml {} \;) do [[ "$vm" =~ "$VM_REGEX" ]] && remove_domain $vm done