Skip to content

Instantly share code, notes, and snippets.

@Bios-Marcel
Last active July 23, 2018 06:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bios-Marcel/ac785e0f52e8e494ec6e06653ceecc9f to your computer and use it in GitHub Desktop.
Save Bios-Marcel/ac785e0f52e8e494ec6e06653ceecc9f to your computer and use it in GitHub Desktop.
function that adds the capability to go back to any directory named X using cd. Just put it into ~/.bashrc
function cdb() {
#suppress all output and run the original cd with all given arguments
command cd $@ &>/dev/null
#If the original ran successfully, do an early exit
if [ $? -eq 0 ]; then
return
fi
START_DIR="$PWD"
while [[ $PWD != '/' && $PWD != **"/$1" ]]; do
command cd ..;
done
if [ "$PWD" = '/' ]; then
command cd "$START_DIR"
command cd "$1"
fi
}
@Bios-Marcel
Copy link
Author

Bug:

cd

doesn't work anymore, instead you have to do:

cd ~

I'll have to fix that.

@Bios-Marcel
Copy link
Author

Instead of having if-branches for every edgecase, i could just run cd with the given input and only apply the special behaviour on failure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment