Last active
November 12, 2015 18:03
-
-
Save chanux/1119556 to your computer and use it in GitHub Desktop.
Go back in directory tree
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
function za() | |
{ | |
## I added this in my bashrc so that I can easily | |
## jump back in my directory tree. | |
## There's probably a better way of doing this. | |
## But I was bored anyway. | |
## Backstory: https://chanux.wordpress.com/2012/08/09/no-more-cd-dot-dot-slash/ | |
## See it in my bashrc: https://github.com/chanux/dotfiles/blob/master/bash/aliases#L25 | |
## Usage: za <number> | |
## Ex: za 2 (this will take you two directories up | |
## from your current working directory. | |
## You can't go beyond your home directory.) | |
## To jump forward: https://github.com/rupa/z | |
ZA_OLD_IFS=$IFS | |
IFS=$(echo -en "\n\b") | |
if [ -z $1 ];then | |
za_mark=1 | |
else | |
za_mark=$1 | |
fi | |
za_dir=$PWD | |
# When in home, do nothing | |
[ $za_dir == $HOME ] && return | |
while [[ $za_mark -gt 0 && $za_dir != $HOME ]];do | |
za_dir=${za_dir%/*} | |
let za_mark-=1 | |
done | |
cd $za_dir | |
IFS=$ZA_OLD_IFS | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment