Created
August 5, 2011 21:17
-
-
Save westonruter/1128549 to your computer and use it in GitHub Desktop.
Bash shortcut function to cd you to an ancestor directory, named as first argument. Stop having to cd ../../../..
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
#!/bin/bash | |
# Usage: cdup [DIRNAME] | |
# Bash shortcut function to cd you to ancestor directory named as first argument. | |
# If DIRNAME is not supplied, then .. is used. | |
# Stop having to cd ../../../.. | |
# Use `cd -` to undo, to restore old working directory. | |
# Use aliases for common directories, like: alias docroot="cdup docroot". | |
# Add function to your ~/.profile | |
# | |
# Example: | |
# $ pwd | |
# > /var/www/html/example.com/docroot/long/path/to/something | |
# $ cdup html | |
# $ pwd | |
# > /var/www/html | |
# $ cd - | |
# $ pwd | |
# > /var/www/html/example.com/docroot/long/path/to/something | |
function cdup { | |
if [ $# == 0 ]; then | |
cd .. | |
return 0 | |
fi | |
dir="$1" | |
old=`pwd` | |
new=`sed s:/$dir/.*:/$dir: <<< $old` | |
if [ "$old" == "$new" ]; then | |
echo "Can't find '$dir' among ancestor directories." 1>&2 | |
return 1 | |
fi | |
cd $new | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
FYI: Latest is going to go here: https://github.com/westonruter/misc-cli-tools/blob/master/cdup