Skip to content

Instantly share code, notes, and snippets.

@zfenj
Last active November 9, 2022 22:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zfenj/43dc107ab0685343d558d2c8024081fd to your computer and use it in GitHub Desktop.
Save zfenj/43dc107ab0685343d558d2c8024081fd to your computer and use it in GitHub Desktop.
Zsh: Get Filename or Extension from Path
# https://zaiste.net/zsh_get_filename_extension_path/#!/bin/zsh
# Filename
fullpath="/etc/nginx/nginx.conf"
filename=$fullpath:t
echo $filename
nginx.conf
# Path
fullpath="/etc/nginx/nginx.conf"
thepath=$fullpath:h
echo $thepath
/etc/nginx
# Filename without extension
fullpath="/etc/nginx/nginx.conf"
filename=$fullpath:t:r
echo $filename
nginx
# Fileextension
fullpath="/etc/nginx/nginx.conf"
ext=$fullpath:t:e
echo $ext
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment