34

I often find myself doing commands like this

cd ../../../../../

Is there a shortcut I can use that will cd ../ n times?

Mike Seeds
  • 461
  • 4
  • 6
  • Are absolute paths a permitted solution? – user535733 Jan 10 '20 at 15:14
  • [Expand dots to dot-dot component](https://github.com/bac0n/bash_completion) –  Jan 10 '20 at 18:03
  • 7
    Does this answer your question? [climb up the directory tree faster](https://askubuntu.com/questions/110922/climb-up-the-directory-tree-faster) - personally I like [geirha's answer](https://askubuntu.com/a/110932/301745) the best. – wjandrea Jan 11 '20 at 02:07
  • 1
    Or this one https://askubuntu.com/a/703701/295286 Pretty much all solutions revolve around the same idea – Sergiy Kolodyazhnyy Jan 13 '20 at 07:57
  • 1
    @wjandrea I voted to close but then retracted my vote since the linked question specifies [[tag:bash]] while Zsh and csh solutions can be found on this page. – Melebius Jan 16 '20 at 10:58
  • @Melebius Also this question is much more popular than the older one now. When I commented they were on about the same level. I'm retracting my close vote. – wjandrea Jan 16 '20 at 12:58
  • Not directly related, but if you're always traveling to a set of common folders you could use a script like [`z`](https://github.com/rupa/z) to let you jump to folders from anywhere. Just `z Documents` to go there, for example – GammaGames Jan 20 '20 at 21:30

9 Answers9

39

No, there is no existing command for this, but it is trivial to write one. Add these lines to your ~/.bashrc file (thanks to D. Ben Knoble for pointing out that the variable should be local):

function ncd(){
  local path=""
  for ((i=1;i<=$1;i++)); do
    path="../$path"
  done
  echo "Moving to $path" >&2
  cd "$path"
}

Save the file, open a new terminal, and you can now move N directories up using ncd N. For example:

$ pwd
/home/terdon/dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9
$ ncd 5
Moving to ../../../../../
$ pwd
/home/terdon/dir1/dir2/dir3/dir4
$ ncd 2
Moving to ../../
$ pwd
/home/terdon/dir1/dir2
terdon
  • 98,183
  • 15
  • 197
  • 293
16

Coming from the Windows world, Alt + Up Arrow navigates to the parent directory in Windows Explorer. So I made something like this in ~/.inputrc:

"\33\33[A": "cd ..\n"

then pressing Alt + Up Arrow moves to the parent directory in the terminal. You have to press multiple times of course to move higher, but I have found it to be very fast. Also you can change the shortcut to your liking.

Zombo
  • 1
  • 21
  • 21
15

You can easily create aliases or shell functions to perform the given task. I personally use these:

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

and so on, manually defined for up to many-many dots in my .bashrc.

A shell function could instead easily take a numeric parameter, and execute cd .. in a loop that many times.

egmont
  • 7,790
  • 1
  • 27
  • 37
8

Similar to @terdon's answer, with slightly more natural usage and output:

# go up
up() {
  local count="${1:-1}"
  local path=../
  while (( --count > 0 )) ; do
    path="$path"../
  done
  echo "cd -- $path"
  cd -- "$path"
}

This defines a function up, which has the following properties:

  • up n will go up n directories
  • up will go up exactly one directory (i.e., the default for n is 1)
  • output mimics that when using shopt -s autocd (hence the --, which are technically unnecessary)
  • as before, OLDPWD is set correctly to the current value of PWD, rather than one of the intermediate directories. This means that up 5; cd - works as expected.
  • Similar? That's the same, only more confusing. – rexkogitans Jan 12 '20 at 13:48
  • 1
    I guess "torek" is me? :) Your addition of `local` makes a lot of sense, I should have thought of that myself! Then, you are defaulting to `cd ../` when no input is passed, which is also good, but you don't actually explain any of these benefits, so they aren't obvious to the novice. The `--` is pointless since you know the argument will always be a variation of `../`. And you could simplify to `while (( --count > 0 )) ; do` to avoid the needless repetition of `(( count-- ))`. But this is indeed a better version than mine, it's a shame you don't point out the benefits. – terdon Jan 12 '20 at 14:16
  • @terdon eek! Sorry I was thinking of torek, who i see in tag:git a lot. Updates coming eventually. – D. Ben Knoble Jan 12 '20 at 15:53
  • @rexkogitans what's confusing? I'd love to know to increase the readability. – D. Ben Knoble Jan 12 '20 at 16:47
  • I admit that a function should declare all variables as `local`. My point in saying it's more confusing is - maybe subjective: `for` is more readable than `while` with a predecrement. Also, what does `${1:-1}` do? If i did not oversee something, that's the same as just `$1`. I would also quote everything, like `path="${path}../"`. – rexkogitans Jan 13 '20 at 08:10
  • *What does `${1:-1}` do*--see `man bash`, and look under Parameter Expansion (*Use Default Values. If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.*) The while is subjective, but I'm not convinced I should change it. Reasonable point about the quotes though – D. Ben Knoble Jan 13 '20 at 14:23
4

Using the printf hack:

function updir(){
    cd $(printf '../%.0s' {1..$1})
}

E.G. updir 5 will move you up 5 directories.

Eric Reed
  • 151
  • 3
2

Another possibility to consider is physically sending the keys if you have xdotool:

xdotool type ../

Assign it to a keyboard hotkey through Keyboard -> Shortcuts dialog. Then you can press the hotkey a required number of times.

stackzebra
  • 635
  • 6
  • 12
2

If you use zsh you can use repeat:

repeat 5 { cd ..}
jesse_b
  • 171
  • 3
2

In Zsh, you can simply add a single dot per level, i.e.

  • cd ... equals to cd ../.. and
  • cd ...... equals to cd ../../../../..

Unfortunately, this cannot be used together with other path components, i.e. commands like

cd ...../foo

don’t work.

See also more tips on the cd command in Zsh.

Melebius
  • 11,121
  • 8
  • 50
  • 77
1

I have used this for years:

alias up=" cd .."
alias up2="cd ../.."
alias up3="cd ../../.."
alias up4="cd ../../../.."
alias up5="cd ../../../../.."
alias up6="cd ../../../../../.."
alias up7="cd ../../../../../../.."
alias up8="cd ../../../../../../../.."
alias up9="cd ../../../../../../../../.."
Alan Thompson
  • 243
  • 2
  • 9