Go up several directories in linux

LinuxCd

Linux Problem Overview


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

Linux Solutions


Solution 1 - Linux

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.

Solution 2 - Linux

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

Solution 3 - Linux

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

Solution 4 - Linux

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.

Solution 5 - Linux

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

Solution 6 - Linux

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

Solution 7 - Linux

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

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

Solution 8 - Linux

for i in {1..7}; do cd ..; done

Solution 9 - Linux

Hm, I don't think so.

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

Solution 10 - Linux

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

Solution 11 - Linux

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.

Solution 12 - Linux

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.

Solution 13 - Linux

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/

Solution 14 - Linux

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`

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser15683854875644328975643872View Question on Stackoverflow
Solution 1 - LinuxFlorin StingaciuView Answer on Stackoverflow
Solution 2 - LinuxGrigory KView Answer on Stackoverflow
Solution 3 - LinuxChakalakaView Answer on Stackoverflow
Solution 4 - LinuxDaniel Thaagaard AndreasenView Answer on Stackoverflow
Solution 5 - LinuxRussellStewartView Answer on Stackoverflow
Solution 6 - LinuxdavView Answer on Stackoverflow
Solution 7 - LinuxMaksim SkurydzinView Answer on Stackoverflow
Solution 8 - LinuxWaleed KhanView Answer on Stackoverflow
Solution 9 - LinuxE. LüdersView Answer on Stackoverflow
Solution 10 - Linuxdoberoi96View Answer on Stackoverflow
Solution 11 - LinuxroubleView Answer on Stackoverflow
Solution 12 - LinuxchrizView Answer on Stackoverflow
Solution 13 - LinuxthathaView Answer on Stackoverflow
Solution 14 - LinuxBlow-OutView Answer on Stackoverflow