Get the latest ZFS snapshot name

In my experiments with FreeNAS and ZFS I came across a need to obtain the name of the latest snapshot of a given dataset. For some odd reason this information is not readily available (that I could find, anyway.) After much googling I finally constructed an answer to my own question, “How do I get the name of the latest ZFS snapshot?”

The answer is via the zfs list command, using the -t, -o, and -r options, and then piping the output to tail to grab the last result.

zfs list -t snapshot -o name -s creation -r storage/Documents | tail -1

Argument breakdown:

  • -t type of ZFS item you want information for
  • -o list of properties of the type above you want to return
  • -s sort by
  • -r specific volume
  • -1 (from tail): only return one line (the last one)

The example above returns the name of the latest snapshot taken from my Documents dataset, which is on my storage volume.

5 thoughts on “Get the latest ZFS snapshot name”

  1. Thanks for the tip, this is quite useful, however I would just note that “-r” parameters also displays any child datasets. I was trying to find the latest snapshot on a parent, so to not show the children, I grepped for the parent via “grep storage/data@”, like this:

    zfs list -t snapshot -o name -s creation -r storage/data | grep storage/data@ | tail -n 1

    1. More bullet-proof is beginning of the line. It covers case when you have pool name in your dataset name (like storage/iscsi/smbfs.storage):

      snapLatest=`zfs list -t snapshot -o name -s creation -r $dataset | grep -e ‘^’$dataset’@’ | tail -1`

      snapOlder=`zfs list -t snapshot -o name -s creation -r $dataset | grep -e ‘^’$dataset’@’ | tail -2 | sort -r | tail -1`

  2. zfs list -r -t snapshot -p -o name -S creation -d 1 -H $dataset | head -1
    -d depth
    Recursively display any children of the dataset, limiting the recursion to depth. A depth of 1 will display only the dataset and
    its direct children.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.