Zsh has neat shortcuts for decomposing file names & paths

I only just learned that the venerable basename and dirname are no longer necessary, let-alone the horrible string splitting required for teasing apart names from file extensions. Zsh has some awesomely convenient expansion modifiers which do it all! e.g. given the path “/Users/me/Documents/Work.zip”:

$file:t → Work.zip

$file:h → /Users/me/Documents

$file:r → /Users/me/Documents/Work

$file:e → zip

You can also compose these arbitrarily, e.g.:

$file:t:r → Work

A full list of expansion modifiers is in the Zsh Modifiers documentation.

Kudos to Zaiste Programming for clueing me into this.

Leave a Comment