Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Convert absolute paths to relative paths



On Apr 19,  7:49am, Vin Shelton wrote:
}
} Can anyone show me some nifty zsh hackery to convert a string
} representing an absolute path into a relative path?  For example, if
} my current $PWD is "/opt/build" and the path I want to convert is
} "/opt/src/perl-5.6.1", I would like the result to be
} "../src/perl-5.6.1".

function relative {
    emulate -L zsh
    local up=.. down
    # ! -d $up/$down accounts for symlinks in $PWD
    while [[ ${PWD#$1} == $PWD || ! -d $up/$down ]]
    do
	up=../$up
	if [[ -n $1:t ]]
	then
	    down=$1:t${down:+/$down}
	    1=$1:h
	fi
    done
    print $up/$down
}

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net   



Messages sorted by: Reverse Date, Date, Thread, Author