ZFS, Bash, and Piped Loops

Fellow Linux Nutjobs! Here is your evening lesson in how to reclaim space from a ZFS partition by destroying snapshots. In my setup, my remote backup has a subset of the snapshots I’m automatically creating from my backup scripts. To reclaim space, I’m deleting those snapshots that have been backed up off-site.

Assume alias Zfs="sudo zfs" in the following example:

Zfs list -rHt snap tank/pictures \
| while read L ; do 
   [[ $L == "tank/pictures@20131008-191254"* ]] && break; 
   echo $L;
done \
| cut -d' ' -f1 \
| while read M ; do 
    Zfs destroy $M; 

Pop quiz. What other command could I use besides cut?

And guess what? I typed that command in almost one go, not pasted in from an editor. I’ve formatted it all fancy-like for your precious sanity.

Example of how Jed works his mojo.

Example of how Jed works his mojo.