Skip to content

Instantly share code, notes, and snippets.

@sathishvj
Created August 24, 2018 10:16
Show Gist options
  • Save sathishvj/a0c02b892bb495eb3c1c593d4672e071 to your computer and use it in GitHub Desktop.
Save sathishvj/a0c02b892bb495eb3c1c593d4672e071 to your computer and use it in GitHub Desktop.
Bash script to go back up to any dir in path
# A very useful (and very much used) bash script I found recently to go back up to any dir in the current path. Supports autocomplete also which is awesome.
# go back up to a directory - https://unix.stackexchange.com/questions/14303/bash-cd-up-until-in-certain-folder
function up()
{
if [ -z "$1" ]; then
return
fi
local upto=$1
cd "${PWD/\/$upto\/*//$upto}"
}
# command line autocomplete
function _up()
{
local cur=${COMP_WORDS[COMP_CWORD]}
local d=${PWD//\//\ }
COMPREPLY=( $( compgen -W "$d" -- "$cur" ) )
}
complete -F _up up
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment