30

Is there any shortcut for cd ..?

10
  • 47
    Does it take so long to type?
    – user36099
    Mar 19, 2011 at 15:08
  • 17
    YES, it does take too long for something needed that often.
    – maaartinus
    Mar 19, 2011 at 16:08
  • 2
    Try push and pop Mar 19, 2011 at 19:44
  • 19
    I'm terribly amused that someone finds cd .. too cumbersome to type
    – Erik
    Mar 20, 2011 at 8:16
  • 8
    I suspect the people who need ‘cd ..’ so often have the “I must cd into a directory before talking about the files there” anti-pattern, rather than specifying filesystem paths.
    – bignose
    Apr 20, 2011 at 7:08

9 Answers 9

69

Not by default (There might be some exceptions to this), there isn't. But if you use the alias command you can create a shortcut like this:

alias ..="cd .."

This will allow you to use the command .. to do cd ...

10
  • 10
    Damn, I was just typing the exact same thing.
    – Majenko
    Mar 19, 2011 at 15:10
  • 6
    I'm using also alias ...="cd ../.." and two more. Additionally, I can recommend alias ,=cd "$OLDPWD".
    – maaartinus
    Mar 19, 2011 at 16:09
  • 13
    @maaartinus: cd -. Mar 19, 2011 at 16:11
  • 1
    @honk I didn't know it. However, it's too long (and I don't need the output).
    – maaartinus
    Mar 19, 2011 at 16:25
  • 1
    Could someone please explain the downvote?
    – Wuffers
    Mar 19, 2011 at 22:24
82

Activate the autocd option. It will let you type .. for cd .. and will actually let you use any directory as a command name and will cd to it:

shopt -s autocd

For the curious, the same exists for zsh:

setopt auto_cd
10
  • 1
    That is fantastic. Time to edit .bashrc on all my linux boxes. Unfortunately it doesn't seem to work with cygwin bash.
    – juggler
    Mar 19, 2011 at 19:42
  • 3
    Woah, that's awesome.
    – Reid
    Mar 19, 2011 at 20:38
  • 5
    @gordoco: yeah, it's bash 4.x only. It's the reason why I've been using zsh for 14 years. Mar 20, 2011 at 1:55
  • 1
    zsh (+ auto_cd) FTW!
    – johnsyweb
    Mar 20, 2011 at 3:17
  • 2
    @LaC: Technical superiority isn't always a determinant of dominance. Bash is far older and has far more written using it, and practically all shell programmers know it, more than anything else. Change can be hard. -- from someone who doesn't know what all the zsh noise is all about, and has only really ever used bash :)
    – tshepang
    Apr 9, 2011 at 8:41
42

I find this useful:

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

For example, up 4 = cd ../../../..

As a bonus, `up 4`/path/to/file works in a similar way to ../../../../path/to/file.

2
  • That's interesting! In 2007 I wrote a similar [function] (home.arcor.de/hirnstrom/minis/index.html#up_X) for my .bashrc - file. Your solution allows to jump back the whole way with cd - too. Well done! :) Mar 20, 2011 at 8:57
  • Nice. I had something like this for VMS DCL when I worked on that, but didn't really think about it when I moved to Unix and its variants.
    – GreenMatt
    Oct 11, 2012 at 13:33
16

After doing a

bind '"\eu":"cd ..\C-m"'

you can cd .. by pressing Alt-u. I used to use this on chdir championships ;-)

If you want the whole swift-chdir suite

ALT+u cd ..
ALT+n enter next dir for selection
ALT+p enter previous dir for selection
ALT+SPC choose selected dir
ALT+l list subdirectories and highlight selection

just include this in your .bashrc

swiftcd-status() {
    echo -n -e "\r\0033[1A\0033[J"
}

swiftcd-contents() {
    find "$swiftcdpath" -maxdepth 1 -executable -type d
}

swiftcd-setdir () 
{ 
    swiftcdpath=$PWD
    swiftcddirnum=1
    swiftcddircount=`swiftcd-contents|wc -l`;
    swiftcd-status
}

swiftcd-pd () 
{ 
    (( --swiftcddirnum <= 0 )) && swiftcddirnum=$swiftcddircount;
    swiftcd-switch $swiftcddirnum
    swiftcd-status
}

swiftcd-updir () 
{ 
    cd ..;
    swiftcd-setdir
}

swiftcd-list () 
{ 
    local redOn="$(echo -e '\0033[31m\0033[1m')"
    local redOff="$(echo -e '\0033[m')"

    swiftcd-status
    ((  swiftcddircount > 1 )) && { 
        echo
        echo $(swiftcd-contents|sed 's!^'"$swiftcdpath"'!!'|sed $swiftcddirnum' s/\(.*\)/'"$redOn"'\1'"$redOff"'/')
    } 
}

swiftcd-nd () 
{ 
    (( ++swiftcddirnum > swiftcddircount )) && swiftcddirnum=1;
    swiftcd-switch $swiftcddirnum
    swiftcd-status
}

swiftcd-switch() {
    (( $1 <= swiftcddircount && $1 > 0 )) && cd $(swiftcd-contents|sed -n $1' p')
}

bind '"\e ":"swiftcd-setdir\C-m"'
bind '"\ep":"swiftcd-pd\C-m"'
bind '"\eu":"swiftcd-updir\C-m"'
bind '"\en":"swiftcd-nd\C-m"'
bind '"\el":"swiftcd-list\C-m"'
swiftcd-setdir
2
  • 4
    What's a chdir championship? ignorant Mar 19, 2011 at 21:57
  • 4
    Directory navigation sports event.
    – wnrph
    Mar 19, 2011 at 22:11
14

I use this one since I type too fast and often miss the space bar:

alias cd..="cd .."
5
  • Most inconvenient for a non-unix person occasionally on a *nix box +1
    – mplungjan
    Mar 19, 2011 at 17:52
  • 11
    s/non-unix/Windows/ Mar 19, 2011 at 19:02
  • 3
    If you are typing so fast you are missing characters, it's hard to believe adding alias for cd.. really helps.
    – Olli
    Mar 20, 2011 at 12:25
  • 3
    I can see how it could help if you come from dear old DOS, where cd.. was valid :)
    – cambraca
    Mar 20, 2011 at 19:02
  • 1
    For some reason I have been typing cd.. a lot for the past week. I thought I was going to have to quit coffee but I like this solution way better. Mar 20, 2011 at 20:35
7

Try autojump: autojump on github

This will not only shorten the cd command into 'j' but also shorten the characters needed for typing the folder you want to jump into.

If you always cd into the folder /home/foo/Projects/thenextfacebook you can just do 'j face' and there you are. It learns automatically which folders are the most important ones for you and after using it for a month now I can say that it is very accurate.

2

I use alias …='cd ..' in my .bashrc file. Unicode rules ;)

1

My bashrc file contains

alias ..="cd .. && ls -lG"

as well as

alias ,,="pushd .. && ls -lG"
1

Try Cd Deluxe for a greatly improved cd command. It supports things like "cd ..." and "cd ...." and so on in order to quickly navigate upwards.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .