Go up multiple levels of directories quickly and easily.

cd() { if [[ "$1" =~ ^\.\.+$ ]];then local a dir;a=${#1};while [ $a -ne 1 ];do dir=${dir}"../";((a--));done;builtin cd $dir;else builtin cd "$@";fi ;}
This is a kind of wrapper around the shell builtin cd that allows a person to quickly go up several directories. Instead of typing: cd ../.. A user can type: cd ... Instead of: cd ../../.. Type: cd .... Add another period and it goes up four levels. Adding more periods will take you up more levels.

2009-10-29 21:43:51
cd cd

2 Alternatives + Submit Alt

What Others Think

By using sed, I can reduce your function by 52 characters and extend it to any number of dots! function cd() { if [[ "$1" =~ "^\.\.+$" ]];then builtin cd "$(echo $1 | sed -e 's/\.//' -e 's/\./..\//g')";else builtin cd "$@";fi }
flatcap · 661 weeks and 1 day ago
flatcap, I had in mind something like that although your version doesn't seem to work for me on Bash 4.0.33. I also have some reservations about using external programs for a shell builtin, so I came up with this test function that converts dots into the requisite number of "../"s. test() { local a dir;a=${#1};while [ $a -ne 1 ];do dir=${dir}"../";((a--));done; echo $dir;} It would be easy enough to incorporate if I could get that test condition to match correctly.
eightmillion · 661 weeks and 1 day ago
I managed to get that test to work. I had to unquote the regular expression. Here's the new command that doesn't use any external programs: cd() { if [[ "$1" =~ ^\.\.+$ ]];then local a dir;a=${#1};while [ $a -ne 1 ];do dir=${dir}"../";((a--));done;builtin cd $dir;else builtin cd "$@";fi ;}
eightmillion · 661 weeks and 1 day ago
flatcap · 661 weeks ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: