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 .. ?

1
  • 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, 2017 at 2:10

14 Answers 14

91
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.

2
  • 4
    cd - command is interesting. Is there any windows equivalent for the same?
    – RBT
    Dec 16, 2016 at 8: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 1, 2017 at 3: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
10

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

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.

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

3

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

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

3
for i in {1..7}; do cd ..; done
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 / $
2
  • best answer for long-term usage
    – Kyle
    Nov 26, 2015 at 6: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 1, 2017 at 3: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.

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.

2
  • 2
    I modified the function so that it only calls cd once. This preserves the ability of cd - to jump back to the previous directory. Jan 5, 2018 at 15:45
  • @mc_electron Can you share that function? Thanks!
    – Niels Bom
    Nov 7, 2022 at 10:25
1

Hm, I don't think so.

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

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/
1
  • I think users should get familiar and used to cd and not external utilities.
    – vmemmap
    Dec 31, 2021 at 11:51
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`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.