Created
June 29, 2019 08:07
-
-
Save mschmitt/12a2113550abe0c82e7688f16efabe01 to your computer and use it in GitHub Desktop.
shorten a given pathname by compressing all /paths/leading/to/pathname into single character /p/l/t/pathname
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
#!/bin/bash | |
# shorten a given pathname by compressing all | |
# /paths/leading/to/pathname into single character /p/l/t/pathname | |
function condensepath { | |
local PATHNAME="$1" | |
local REGEX='(.*/)(.)[^/]+(/.+)' | |
# https://stackoverflow.com/a/22261454 | |
while [[ "$PATHNAME" =~ $REGEX ]] | |
do | |
PATHNAME="${BASH_REMATCH[1]}${BASH_REMATCH[2]}${BASH_REMATCH[3]}" | |
done | |
echo "$PATHNAME" | |
} | |
FULLPATH="/paths/leading/to/pathname" | |
printf "%s -> %s\n" "$FULLPATH" "$(condensepath "$FULLPATH")" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment