59

When I want to go one directory up I use

cd ..

But when I want to go 7 directories up is there any way to do this other than just typing seven (7) times cd .. ?

ealfonso
  • 6,622
  • 5
  • 39
  • 67
  • On a side note, to go one level up both `cd..` and `cd ..` are equivalent in windows but linux is too strict and rejects `cd..` command as invalid command just because of missing space. – RBT Feb 28 '17 at 02:10

14 Answers14

89
cd ../../../../../../../

Also another useful navigation tip is if for example lets say you keep switching from a directory (call it A) to another (call it B) that's 7 directories up, in your case.

So if you're in directory A:

A> cd ../../../../../../../
B> // Now you're in directory B and want to go back to A
B> cd -

That will move right back to directory A. - expands to the previous directory you were in.

Florin Stingaciu
  • 8,085
  • 2
  • 24
  • 45
  • 4
    `cd -` command is interesting. Is there any windows equivalent for the same? – RBT Dec 16 '16 at 08:31
  • 1
    Although this is the "accepted" answer, I see that @dav's answer below is the more robust solution. A long list of "../" is both onerous and prone to error. If your profile allows an alias script, it is more scalable. The bash alias version also preserves support for "cd -". – ZaydH Oct 01 '17 at 03:08
29

Make alias (in you ~/.bashrc)

function cd_up() {
  cd $(printf "%0.0s../" $(seq 1 $1));
}
alias 'cd..'='cd_up'

and use:

$ cd.. 7

UPD: Or make more powerfull variant, cd to dir name in current path:

# cd up to n dirs
# using:  cd.. 10   cd.. dir
function cd_up() {
  case $1 in
    *[!0-9]*)                                          # if no a number
      cd $( pwd | sed -r "s|(.*/$1[^/]*/).*|\1|" )     # search dir_name in current path, if found - cd to it
      ;;                                               # if not found - not cd
    *)
      cd $(printf "%0.0s../" $(seq 1 $1));             # cd ../../../../  (N dirs)
    ;;
  esac
}
alias 'cd..'='cd_up'                                # can not name function 'cd..'

use:

$ cd /home/user/documents/projects/reports/2014-10-01
$ cd.. doc
$ pwd
> /home/user/documents
Grigory K
  • 1,301
  • 10
  • 10
10

you can use pushd . to remember one directory and popd to go back to it.

Chakalaka
  • 2,807
  • 19
  • 26
8

If there is a command I use a lot I will just make an alias.

You could type

alias ..='cd ..'
alias ...='cd ../..'

Then you can just use .. to go one level up and ... to go two levels up.

RussellStewart
  • 5,293
  • 3
  • 26
  • 23
5

Here is a slight improvement I have found:

Usually, when you go back one directory with cd .., you end up going forward one directory after. To make this work, you have to use functions rather than aliases, so instead of:

alias ..=”cd ../”
alias ..2=”cd ../../”

you can use:

..()
{
cd ../$@
}
..2()
{
cd ../../$@
}

However, after using this, it quickly becomes apparent that you are missing command completion for this function, making it far less useful than it could be. Thus, I went back and added my own bash completion functions for this function, which can be pasted into your ~/.bashrc or any file (e.g. ~/.bash_completion) that is called from your ~/.bashrc if you wish to avoid clutter. Here is the completion code:

_..()
{
local cur=../${COMP_WORDS[COMP_CWORD]}
COMPREPLY=( $(compgen -d — $cur) )
local i=${#COMPREPLY[*]}
while [ $((--i)) -ge 0 ]; do
COMPREPLY[$i]=$(echo ${COMPREPLY[$i]} | sed -r ‘s/(\.\.\/)*//’)
done
}
complete -F _.. -o nospace -S / ..

_..2()
{
local cur=../../${COMP_WORDS[COMP_CWORD]}
COMPREPLY=( $(compgen -d — $cur) )
local i=${#COMPREPLY[*]}
while [ $((--i)) -ge 0 ]; do
COMPREPLY[$i]=$(echo ${COMPREPLY[$i]} | sed -r ‘s/(\.\.\/)*//’)
done
}
complete -F _..2 -o nospace -S / ..2
RussellStewart
  • 5,293
  • 3
  • 26
  • 23
5

Do not remember from where I copied this function, but it works great for me (you can put in .bashrc file for example)

up() { local p= i=${1:-1}; while (( i-- )); do p+=../; done; cd "$p$2" && pwd; }

e.g. to go 7 directories up it is necessary to type up 7

dav
  • 8,931
  • 15
  • 76
  • 140
3

You can do it like this cd ../../../../../../../

There is a cool article about some hacks you can apply to improve you navigation.

Maksim Skurydzin
  • 10,301
  • 8
  • 40
  • 53
3
for i in {1..7}; do cd ..; done
Waleed Khan
  • 11,426
  • 6
  • 39
  • 70
3

You could add the following function to your .bashrc:

# ~/.bashrc
up() {
  for D in $(seq 1 $1);
     do 
       cd ..
     done
}

So the usage would simply be:

user ~ $ up 3
user / $
doberoi96
  • 413
  • 6
  • 22
  • best answer for long-term usage – Kyle Nov 26 '15 at 06:37
  • Including "and" seems to not work. I got it to work by deleting "and". The approach from @dav below allows "cd -" to still work so I would say it is the better long term answer. – ZaydH Oct 01 '17 at 03:02
2

This nifty function supports going up in both directions.

If you are in /a/b/c/d then 'up 1' will take you to /a/b/c, and so on. Thats pretty standard and covered in most of the other answers.

Now, for the special part; using negative numbers takes you up from the other direction. So, if you are in /a/b/c/d, 'up -1' will take you to /a, and so on.

function up() {
    UP=$1

    if [[ $UP =~ ^[\-0-9]+$ ]]; then
        if ((UP<0)); then
            UP=${UP#-}
            cd $(echo $PWD | cut -d/ -f1-$((UP+1)))
        else
            cd $(printf "%0.s../" $(seq 1 ${UP}));
        fi
    fi
}

Available for download/install on github.

rouble
  • 16,364
  • 16
  • 107
  • 102
2

for fish users,

function cdd
   for x in (seq $argv)
     cd ..
   end
end

and save it to your: ~/.config/fish/functions/cdd.fish

and just $ cdd 3 To go up 3 directories.

chriz
  • 1,826
  • 2
  • 24
  • 28
1

Hm, I don't think so.

But you can write cd ../../../.. (and so on) instead of 7 times cd ..

E. Lüders
  • 1,495
  • 1
  • 12
  • 27
1

I've made a small utility that makes navigating deep directory structures a bit easier: https://github.com/ianatha/up.

With up installed, you could:

$ pwd
/Users/jdoe/Developer/backend/src/main/java/com/example/service/db/
$ up example
$ pwd
/Users/jdoe/Developer/backend/src/main/java/com/example/
$ up 4
$ pwd
/Users/jdoe/Developer/backend/src/
thatha
  • 307
  • 3
  • 9
0

You may try this solution. It is work fine for me:

#!/bin/bash

#this sctipt should take one parameter - number of steps. And do "cd" command step times.
# example usage: cd `cde 3` 

NEW_PATH=$(pwd)

step="../"

upper=""

for i in seq $1  
do 
        upper="$upper$step"
done

echo $upper

I have made alias in ~/.barsh file. And this work fine for me.
Example, up for three folders.
$cd `cde 3`

Blow-Out
  • 1
  • 1