Created
February 3, 2011 14:21
-
-
Save kotas/809522 to your computer and use it in GitHub Desktop.
cd up-forward
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
# INSTALL: | |
# alias up='source /path/to/up.sh' | |
# | |
# USAGE: | |
# $ up = cd .. | |
# $ up 2 = cd ../.. | |
# $ up 3 = cd ../../.. | |
# $ up foo = cd to the nearest `foo' directory in the ancestor path. | |
# ex) At /foo/bar/baz/ , then cd to /foo/ | |
# ex) At /foo/bar/foo/baz/ , then cd to /foo/bar/foo/ | |
# ex) At /bar/baz/ , then shows error | |
# $ up a/b = cd to the nearest `a/b' path in the ancestor path. | |
# ex) At /a/b/c/d/ , then cd to /a/b/ | |
# | |
# Note: `up 2` at /1/2/3/4/5/ is `cd /1/2/`, not `cd ../..`. | |
# | |
# LICENSE: | |
# Public domain. Use this at your own risk. | |
$( | |
/usr/bin/env perl -e ' | |
my ($curdir, $target) = @ARGV; | |
if ($target eq "") { | |
print "cd .."; | |
} elsif ((my $moveto = "$curdir/") =~ s|^(.*/$target/).+$|$1|) { | |
print "cd ", $moveto; | |
} elsif ($target =~ /^\d+$/) { | |
print "cd ", ("../" x $target); | |
} else { | |
print STDERR "No such directory: $target\n"; | |
} | |
' `pwd` "$1" | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment