Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
cd .. with less fuss
# cd .. with multiple jumps or jump up to name
..() {
if [ "-" = "$1" ]; then cd -; return; fi; # return to previous directory
if [ "/" = "$1" ]; then cd /; pwd; return; fi; # jump to root
if [ -z "$1" ]; then cd ../; pwd; return; fi; # jump up one
declare -i count=$1; # get a jump count
if [ $count -eq 0 ]; then # wasn't a number, look for name
local go=$(while [ "/" != "$PWD" ] && [ "$(basename $PWD)" != "$1" ]; do cd ..; done; pwd);
# jump up to named directory, or don't move if name wasn't found
if [ "/" != "$go" ]; then cd $go; else return; fi;
else
if [ $count -gt 255 ]; then count=1; fi; # sensible limits
# jump up count directories
cd $(for i in $(eval echo {1..$count}); do echo -n "../"; done);
fi;
pwd; # behave like "cd -" by echoing current directory
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.