Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nfekete/5956fd64687940c4019dd084ff2834a9 to your computer and use it in GitHub Desktop.
Save nfekete/5956fd64687940c4019dd084ff2834a9 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
# result: nginx.conf
# Path
fullpath="/etc/nginx/nginx.conf"
thepath=$fullpath:h
echo $thepath
# result: /etc/nginx
# Filename without path and extension
fullpath="/etc/nginx/nginx.conf"
filename=$fullpath:t:r
echo $filename
# result: nginx
# Filename with path without extension
fullpath="/etc/nginx/nginx.conf"
filenameandpath=$fullpath:r
echo $filenameandpath
# result: /etc/nginx/nginx
# Fileextension
fullpath="/etc/nginx/nginx.conf"
ext=$fullpath:t:e
echo $ext
# result: conf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment