Skip to content

Instantly share code, notes, and snippets.

@kotas
Created February 3, 2011 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kotas/809522 to your computer and use it in GitHub Desktop.
Save kotas/809522 to your computer and use it in GitHub Desktop.
cd up-forward
# 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