From e86dbb196d0947b2aa2477b8ea0c745b8d06a131 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 19 Sep 2019 11:36:27 +0300 Subject: [PATCH] docs: update --- README.md | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/README.md b/README.md index b4ccc62..d225565 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,9 @@ A collection of pure POSIX `sh` alternatives to external processes * [Get the number of lines in a file](#get-the-number-of-lines-in-a-file) * [Count files or directories in directory](#count-files-or-directories-in-directory) * [Create an empty file](#create-an-empty-file) +* [FILE PATHS](#file-paths) + * [Get the directory name of a file path](#get-the-directory-name-of-a-file-path) + * [Get the base-name of a file path](#get-the-base-name-of-a-file-path) @@ -245,3 +248,52 @@ Alternative to `touch`. # OR (shellcheck warns for this) >file ``` + +# FILE PATHS + +## Get the directory name of a file path + +Alternative to the `dirname` command. + +**Example Function:** + +```sh +dirname() { + # Usage: dirname "path" + printf '%s\n' "${1%/*}/" +} +``` + +**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" + path=${1%/} + printf '%s\n' "${path##*/}" +} +``` + +**Example Usage:** + +```shell +$ basename ~/Pictures/Wallpapers/1.jpg +1.jpg + +$ basename ~/Pictures/Downloads/ +Downloads +```