Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm constantly going 'cd ../../../../'. Is there a command/alias that could let me go 'cmd 4' and I'd be taken back 4 directories?

share|improve this question

migrated from May 10 '11 at 22:55

This question came from our site for professional and enthusiast programmers.

ls? Surely you mean cd? – Chris Jester-Young May 10 '11 at 14:04
Yeah I do, I just wrote this in a rush ;) Editing it now. – Muu May 10 '11 at 14:05
Wiil this do?: alias cmd5 = "cd ../../../../../" – ypercubeᵀᴹ May 10 '11 at 14:05
Well, I suppose I could write them out for 1 to 10 or so, but it'd be nice to have a clever way of doing it. That doesn't really sit right with me. Will do that if no other solution emerges, though. – Muu May 10 '11 at 14:07
up vote 14 down vote accepted

Put this in your ~/.bashrc:

cdup() {
  while ((levels--)); do
    cd ..

(The name cdup comes from the corresponding FTP command, just FYI.)

share|improve this answer
Thanks for the response, went with the jleedev's answer due to it being 1 line shorter and I couldn't make up my mind who's to choose! Thanks anyway! – Muu May 10 '11 at 14:12
@Muu: Both answers will work, but mine will work for many, many levels, and @jleedev's will only work for a small handful (whatever the stack limit is). ;-) (I'm sure you'll never hit the limit either way, but mine is more general, is what I'm trying to get at. :-)) – Chris Jester-Young May 10 '11 at 14:13
Fair point, I've awarded you the answer then. Sorry jleedev - though most likely I'll continue using your solution as I'll forget to change it :) – Muu May 10 '11 at 22:00

I was taught to use 'pushd' and 'popd' for such circumstances.

For example, type 'pushd .' and then 'cd /home'. Now type 'popd' and you will be back to where you started.

'pushd'/'popd' is a stack, you can push as many directories on there as you like, but it is last on, first off when you popd.

share|improve this answer
Hm, nice. Not heard of that. Doesn't quite fit what I was looking for, but will use that too. Thanks. – Muu May 10 '11 at 14:10

Sure, why not:

up() {
    [ $# = 0 ] && cd .. && return
    [ $1 = 0 ] && return
    cd .. && up $(($1 - 1))
share|improve this answer
Bash isn't tail-recursive, right? You might stack overflow if you do up 500. :-) – Chris Jester-Young May 10 '11 at 14:08
If your directories are nested that deeply, you have my sympathies. – Josh Lee May 10 '11 at 14:09
Used this. Beat Chris by 1 line ;) – Muu May 10 '11 at 14:10
@Muu: I didn't know this question was a code golf! ;-) BTW, come join us! – Chris Jester-Young May 10 '11 at 14:12
@Muu Please remember that recursion is not the proper way to write shell scripts in general. – Josh Lee May 10 '11 at 14:17

Quick and dirty:

cmd () { dir=.; for i in $(seq 1 $1); do dir=$dir/..; done; pushd $dir; }

Formulated to only change directory once.

share|improve this answer

Here is an alternative way:

function cdup
    cd $(for ((i=0 ; i<$1 ;i++)); do printf "../" ; done)
share|improve this answer

You might want to look into using pushd and popd to set location markers and and get back there easily from whatever directory you changed to.

You could also set a variable with the name of a directory you use a lot, then CD there:

proj1='cd $MYPROJ'
share|improve this answer

I use autojump which allow me to type :

j h
>> /home/cjulien
j et
>> /etc

it can learn from your habits and is very light (and addictive ;) )

share|improve this answer
Interesting, might check this out. Thanks. – Muu May 10 '11 at 14:16

Two thoughts that might be of use to you:

  1. cd -
    change back to the last directory you were in. I use this all the time to get from deep down in my code tree back to the root of it... though that only works if you cd'd down into it in one change.

  2. cdd old new
    this was a script function I had written in the past that I used for moving around between similar trees. Basically it took two arguments and did a regexp on your path, great for moving around between branches and such. For example if you were in ~/bzr/web/trunk/path/to/feature and needed to get to ~/bzr/web/feature-branch/path/to/feature then you would do cdd trunk feature-branch. The implementation I had was in an archaic shell language on AIX, but it was pretty trivial, so should be easy to implement in shell of choice if you need it. For us it was very useful because the structure looked like: .../{product}/{version}/{release}/{src,bld,dbg,pkg,tst}/{component}/... so moving around in that tree got insane pretty quick.

share|improve this answer

You must log in to answer this question.