14

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?

4
  • ls? Surely you mean cd? May 10, 2011 at 14:04
  • Yeah I do, I just wrote this in a rush ;) Editing it now.
    – Muu
    May 10, 2011 at 14:05
  • Wiil this do?: alias cmd5 = "cd ../../../../../" May 10, 2011 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, 2011 at 14:07

9 Answers 9

16

Put this in your ~/.bashrc:

cdup() {
  levels=${1-1}
  while ((levels--)); do
    cd ..
  done
}

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

3
  • 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, 2011 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. :-)) May 10, 2011 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, 2011 at 22:00
11

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.

1
  • Hm, nice. Not heard of that. Doesn't quite fit what I was looking for, but will use that too. Thanks.
    – Muu
    May 10, 2011 at 14:10
10

Sure, why not:

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

Quick and dirty:

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

Formulated to only change directory once.

2

Here is an alternative way:

function cdup
{
    cd $(for ((i=0 ; i<$1 ;i++)); do printf "../" ; done)
}
1

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:

MYPROJ=~/projects/name/src
cd $MYPROJ
proj1='cd $MYPROJ'
1

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 ;) )

0
1

Build the path using printf then cd to it:

cdup() {
    local path
    printf -v path '%*s' "${1:-1}"
    cd "${path// /../}"
}

Use as:

cdup 4 # to go up four directories
cdup 1 # to explicitly go up one directory
cdup   # to implicitly go up one
0

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.

You must log in to answer this question.