zsh: glob qualifiers: most recent
β’ 234 words β’ 2 min β’ updated
TIL via Adam Johnson:
Zsh: select generated files with (
om[1]) glob qualifiers[…]
In Zsh, we can avoid copying such generated filenames by using glob qualifiers, which are extra syntax you can add to filename globs in Zsh. Specifically, we want to attach the glob qualifiers
om[1], which mean:β
o: sort the matches βm: by modification time, most recent first β[1]: select only the first matchAttaching these qualifiers to appropriate globs allows commands to pick up a generated file without knowing its exact name.
This is akin to ls -t | head -n 1. For example, here are both commands in the
repository of this blog:
% ls -r -t content/posts/* | head -n 1
content/posts/2026-02-15-zsh-glob-qualifiers-most-recent.md-r is --reverse, so that we get the most recent post instead of the oldest
one.
Now with zsh glob qualifiers:
% ls content/posts/*.md(om[1])
content/posts/2026-02-15-zsh-glob-qualifiers-most-recent.mdYou could also fetch the two most recent posts:
% ls content/posts/*.md(om[1,2])
content/posts/2026-02-13-new-blog-post-via-claude-code.md
content/posts/2026-02-15-zsh-glob-qualifiers-most-recent.mdOr perhaps only the second most recent post:
% ls content/posts/*.md(om[2])
content/posts/2026-02-13-new-blog-post-via-claude-code.mdListing files isn’t very interesting. Here’s a more practical use case: delete the most recent file:
% rm *(om[1])Or open the most recent file in $EDITOR:
$EDITOR content/posts/*.md(om[1])om operates by time of last modification, but there’s also on for
alphabetical sorting and oL for file size (length) sorting.
Refer to the zsh docs for more.