cd up a number of levels

function ..(){ for ((j=${1:-1},i=0;i<j;i++));do builtin cd ..;done;}
Instead of typing "cd ../../.." you can type ".. 3". For extremely lazy typists, you can add this alias: alias ...=".. 2" ....=".. 3" - so now you can write just .... !!! NB the .. function needs to be "source"d or included in your startup scripts, perhaps .bashrc.

4
By: bhepple
2010-01-02 08:36:12
cd cd

What Others Think

Nice idea! However, I'm not sure to get the reason why you use 'j=${1:-1}'. Is this parameter expansion trick useful? Is the new variable j useful? Please help me to understand :-)
Josay · 830 weeks and 5 days ago
That's quite fun. Alternatively (not equivalent but simplistically) : alias :="cd .." alias :.="cd ../.." alias ::="cd ../../.." alias ::.="cd ../../../.." # repeat until bored :-) -so now you can write just :: !!! :-)
mpb · 830 weeks and 5 days ago
{VAR:-VAL} means use $VAR if it's non-null, otherwise use VAL - think of it as a default.
bhepple · 830 weeks and 5 days ago
I _like_ ':.' etc, what a hoot.
bhepple · 830 weeks and 5 days ago
Hmmm php ate my '$' - that should have been: ${VAR:-VAL} means use $VAR if it's non-null, otherwise use VAL - think of it as a default.
bhepple · 830 weeks and 5 days ago
it might help to add 'o=$PWD' and 'OLDPWD=$o' at the beginning and end in order for 'cd -' to work as expected
patrix · 830 weeks and 4 days ago
..() { cd "$(seq ${1:0} | sed '/.*/s,,../,' | tr -d '\n')"; } This way, you also keep your "cd -" working.
taliver · 830 weeks and 4 days ago
Actually make that: ..() { cd "$(seq ${1:-1} | sed '/.*/s,,../,' | tr -d '\n')"; }
taliver · 830 weeks and 4 days 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: