Created
September 30, 2011 23:45
-
-
Save j0hn/1255353 to your computer and use it in GitHub Desktop.
Get a short CWD
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/sh | |
# Bash script to transform a long cwd path to a short path | |
# that shows the first 2 folders, and the last 2 folders | |
# | |
# For example: | |
# /media/Datos/j0hn/Code/py/guicavane/guicavane/Downloaders | |
# Will be transformed to: | |
# /media/Datos/.../guicavane/Downloaders | |
# Authors: j0hn, Roger | |
short_cwd () | |
{ | |
# Simpler version using just slices | |
# dir=`echo $PWD | sed "s#$HOME#~#"` | |
# dirlength=${#dir} | |
# if [ "$dirlength" -gt "15" ]; then | |
# dir=${dir:0:10}...${dir:(-10)} | |
# fi | |
# echo $dir | |
BIFS=$IFS; IFS="/"; declare -a Array=(${PWD:1}); IFS=$BIFS | |
dir= | |
len=${#Array[@]} | |
if [ "$len" -gt 5 ]; then | |
for ((i=0;i<=1;i++)); do | |
dir="$dir/${Array[$i]}"; | |
done | |
dir=$dir/... | |
for ((i=$len-2;i<=$len-1;i++)); do | |
dir="$dir/${Array[$i]}"; | |
done | |
else | |
dir=$PWD | |
fi | |
dir=`echo $dir | sed "s#$HOME#~#"` | |
echo $dir; | |
} | |
echo $(short_cwd) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment