# FILE PATHS ## Get the directory name of a file path Alternative to the `dirname` command. **Example Function:** ```sh dirname() { # Usage: dirname "path" dir=${1:-.} dir=${dir%%${dir##*[!/]}} [[ "${dir##*/*}" ]] && dir=. dir=${dir%/*} dir=${dir%%${dir##*[!/]}} printf '%s\n' "${dir:-/}" } ``` **Example Usage:** ```shell $ dirname ~/Pictures/Wallpapers/1.jpg /home/black/Pictures/Wallpapers $ dirname ~/Pictures/Downloads/ /home/black/Pictures ``` ## Get the base-name of a file path Alternative to the `basename` command. **Example Function:** ```sh basename() { # Usage: basename "path" ["suffix"] local tmp tmp=${1%${1##*[!/]}} tmp=${tmp##*/} tmp=${tmp%"${2/"$tmp"}"} printf '%s\n' "${tmp:-/}" } ``` **Example Usage:** ```shell $ basename ~/Pictures/Wallpapers/1.jpg 1.jpg $ basename ~/Pictures/Wallpapers/1.jpg .jpg 1 $ basename ~/Pictures/Downloads/ Downloads ```