Last active
June 1, 2023 17:03
-
-
Save NerdyDeedsLLC/b62a6b377b42be4d04f1d6969594d4af to your computer and use it in GitHub Desktop.
BASH: Abridges & Shortens a FIle Path (e.g. flnm "/some/long/path/to/my/file/location/file.ext" 1 1 will produce "/some/.../location/file.ext")
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# flnm - Breaks apart a file's path by "/" and returns only the number of folder specified for each side, | |
# followed by the file name. | |
# | |
# usage: flnm <filename> <leading elements> <trailing elements> | |
# if <leading elements> is provided and <trailing elements> is omitted, then returns ONLY the back n portions | |
# if <leading elements> is provided and <trailing elements> is omitted, then returns ONLY the file nane | |
# if <leading elements> + <trailing elements> exceeds the file's actual path length, then returns full file path | |
# if both <leading elements> and <trailing elements> are omitted, it assumes a value of 1 for each | |
# | |
# examples: | |
# flnm "/some/long/path/to/my/file/location/file.ext" 1 1 | |
# > /some/.../location/file.ext | |
# | |
# flnm "/some/long/path/to/my/file/location/file.ext" 1 2 | |
# > /some/.../file/location/file.ext | |
# | |
# flnm "/some/long/path/to/my/file/location/file.ext" 2 1 | |
# > /some/long/.../location/file.ext | |
# | |
# flnm "/some/long/path/to/my/file/location/file.ext" | |
# > /some/.../location/file.ext | |
# | |
# flnm "/some/long/path/to/my/file/location/file.ext" 0 | |
# > file.ext | |
# | |
# flnm "/some/long/path/to/my/file/location/file.ext" 0 0 | |
# > file.ext | |
# | |
# flnm "/some/long/path/to/my/file/location/file.ext" 1 | |
# > .../location/file.ext | |
# | |
# flnm "/some/long/path/to/my/file/location/file.ext" 10 20 | |
# > /some/long/path/to/my/file/location/file.ext | |
function flnm { | |
fileName="$1" | |
headCount="$2" | |
tailCount="$3" | |
pathCount=$(( "$(tr -cd "/" <<< $1 | wc -c)" + 1 )) | |
[[ "$2" != "" && "$3" != "" ]] && headCount="$2" && tailCount="$3" | |
[[ "$2" != "" && "$3" == "" ]] && tailCount="$2" && headCount=0 | |
[[ "$headCount" == "" ]] && headCount=1 | |
[[ "$tailCount" == "" || "$tailCount" == "0" ]] && tailCount=1 || tailCount=$(( $tailCount + 1 )) | |
totalCount=$(( $headCount + $tailCount )) | |
[[ "$pathCount" -le "$totalCount" ]] && echo "$fileName" && return 0 | |
headName="$(cut -d "/" -f "1-$headCount" <<< $fileName 2>/dev/null)" | |
tailName="$(echo "$fileName" | tr "/" "\n" | tail -n${tailCount} | tr "\n" "/")" | |
if [[ "$headName" != "" ]]; then | |
echo "$headName/.../${tailName:0:-1}" | |
else | |
[ ! $tailCount -eq 1 ] && echo ".../${tailName:0:-1}" || echo "${tailName:0:-1}" | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice and clean!