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

share|improve this question
    
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 2:10

13 Answers 13

up vote 46 down vote accepted
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.

share|improve this answer
3  
cd - command is interesting. Is there any windows equivalent for the same? – RBT Dec 16 '16 at 8:31
    
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 '17 at 3:08

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.. proj
$ pwd
> /home/user/documents/projects
share|improve this answer

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

share|improve this answer

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
share|improve this answer

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.

share|improve this answer

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

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

share|improve this answer

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

share|improve this answer
for i in {1..7}; do cd ..; done
share|improve this answer

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 / $
share|improve this answer
    
best answer for long-term usage – Kyle Nov 26 '15 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 '17 at 3:02

Hm, I don't think so.

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

share|improve this answer

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`

share|improve this answer

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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