Skip to content

Instantly share code, notes, and snippets.

@mschmitt
Created June 29, 2019 08:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mschmitt/12a2113550abe0c82e7688f16efabe01 to your computer and use it in GitHub Desktop.
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
#!/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