Skip to content

Instantly share code, notes, and snippets.

@geonnave
Last active August 29, 2015 14:13
Show Gist options
  • Save geonnave/70fde701886f9afb59c0 to your computer and use it in GitHub Desktop.
Save geonnave/70fde701886f9afb59c0 to your computer and use it in GitHub Desktop.
Easier way to navigate up directories using bash
# function navigate_up -> navigate up N directories
# param $1 -> must be a number, indicate how many directories to navigate up;
# if omitted, navigate up only one directory
# other parameters -> ignored
navigate_up() {
# if there aren't parameters, navigate up only 1 directory and stop
[ -z "$1" ] && cd .. && return 0
# filter parameter $1, should contains only numbers
non_number_chars=$( echo $1 | sed "s/[0-9]*//g" )
if [ ! -z "$non_number_chars" ]; then
echo -e "You must specify a number of directories to navigate up"
return 1
fi
dirs_up=
for i in $(seq $1); do
dirs_up+="../"
done
cd $dirs_up
return 0
}
# bash doesn't let us to use dots in a function name, so alias is our friend:
alias ..="navigate_up"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment