Al Sweigart's tumblr

Short blog posts on my random ideas and thoughts.

Mar 21

Command line idea: “up”

I had an idea for a piece of software that would be useful to have on the command line. Several times I’ve wanted to jump up several directories from my current working directory, for example: from C:\mgmt\framework\src\lib\python\scripts to C:\mgmt\framework\src

Ordinarily, this would require using “cd ../../..” but that’s a lot to type out. I’d rather just type “up 3”. So I’ve also come up with some other shortcuts that this “up” command could have:

> up

This would be the equivalent of “cd ..”

> up 3

This would be the equivalent of “cd ../../..”

> up bin

This would keep jumping up directories until it found a directory that had a directory named “bin” (or started with “bin”) in it. For example, from c:\foo\bar\bazz to c:\foo\bin. (Or possibly, from c:\zap\blam\foo to c:\zap\binaries)

If there was some ambiguity as to which directory to go in, then it would go into the directory named exactly “bin”. Otherwise, it would display the multiple directory names (just like tab completion does.)

> up bin\lib

This would do the same jumping up until it found a directory named (or began with) “bin”, but furthermore had a directory named (or began with) lib inside of it.

> up 2 bin

This would jump up two directories before starting to look for a directory named (or beginning with) “bin”. For example, if you were in c:\foo\bar\fizz and there was a c:\bin and a c:\foo\bin, it would go to c:\bin. (However, just “up bin” would have resulted in changing directories to c:\foo\bin)

> up bin*ies?

In this case, the command can use the standard * and ? glob pattern matching.

What do you think? Does a command like this already exist? It would be fairly simple to create portable ANSI C code that does this. Would this command even be useful?