Any shortcut for "cd .."?

30

14

Is there any shortcut for cd ..?

user35538

Posted 2011-03-19T15:06:40.560

Reputation: 1 207

47Does it take so long to type? – rightfold – 2011-03-19T15:08:41.713

17YES, it does take too long for something needed that often. – maaartinus – 2011-03-19T16:08:33.993

Related – Paused until further notice. – 2011-03-19T16:19:18.603

if you're typing it multiple times then you might want to do cd\ which is a shorthand for the more correct cd \ (i.e. cd space backslash) – barlop – 2011-03-19T17:58:01.683

@barlop: The question is about bash, not about Windows cmd. – user1686 – 2011-03-19T19:03:10.533

2Try push and pop – Zachary Scott – 2011-03-19T19:44:02.833

19I'm terribly amused that someone finds cd .. too cumbersome to type – Erik – 2011-03-20T08:16:10.247

1you can make an alias ".."="cd .." – jokoon – 2011-03-20T16:41:39.190

8I 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 – 2011-04-20T07:08:28.230

@barlop: OT: DOS/Windows' cd\ goes to the root of top volume (the one with assigned letter), it doesn't go to the parent directory. cd.. does it and yes, you don't have to type space, so theoretically you save a lot of typing. But, in practice, cmd.exe is so rarely used in Windows compared to /bin/sh in *nix, that gain is really negligible. – przemoc – 2011-05-06T08:17:12.443

Answers

68

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

Wuffers

Posted 2011-03-19T15:06:40.560

Reputation: 16 645

10Damn, I was just typing the exact same thing. – Majenko – 2011-03-19T15:10:08.677

6I'm using also alias ...="cd ../.." and two more. Additionally, I can recommend alias ,=cd "$OLDPWD". – maaartinus – 2011-03-19T16:09:53.667

13@maaartinus: cd -. – Benjamin Bannier – 2011-03-19T16:11:09.117

1@honk I didn't know it. However, it's too long (and I don't need the output). – maaartinus – 2011-03-19T16:25:08.097

1Could someone please explain the downvote? – Wuffers – 2011-03-19T22:24:23.620

The downvote wasn't me, but it could be because some distros do include the alias by default. I think Ubuntu might, but I'm not 100% sure. I don't think that warrants a down-vote though. – jmort253 – 2011-03-20T04:40:39.450

@Mark: I upvoted, so I can't answer your question. But anyway, if someone thinks it's bad answer for some reason, he/she is free to downvote. Leaving explanation for downvoting is optional. Of course, it's kind of unethical to downvote competing answers just to get better chances for getting accepted answer. – Olli – 2011-03-20T12:24:21.067

@Olli: I know that commenting on downvotes is optional. I just want to know why they thought it was a bad answer, so I can improve on it. – Wuffers – 2011-03-20T14:07:54.990

1@Mark: I didn’t downvote either (actually I upvoted), but it could be because they prefer the autocd strategy. – sam hocevar – 2011-03-20T14:25:02.500

This is standard on zsh – polemon – 2011-03-20T21:06:16.707

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

sam hocevar

Posted 2011-03-19T15:06:40.560

Reputation: 1 261

1That is fantastic. Time to edit .bashrc on all my linux boxes. Unfortunately it doesn't seem to work with cygwin bash. – juggler – 2011-03-19T19:42:42.587

3Woah, that's awesome. – Reid – 2011-03-19T20:38:40.880

5@gordoco: yeah, it's bash 4.x only. It's the reason why I've been using zsh for 14 years. – sam hocevar – 2011-03-20T01:55:40.180

1zsh (+ auto_cd) FTW! – Johnsyweb – 2011-03-20T03:17:46.063

That's odd, I'm running bash and shopt -s autocd doesn't work. Any ideas why? – Wuffers – 2011-03-20T14:11:33.710

Why isn't zsh more popular, anyway? All of the cool features bash has been gaining were in zsh first (this, programmable completions...), yet no OS or distro uses zsh as the default shell. Why is that? – LaC – 2011-03-20T14:30:21.013

@Mark: I believe this feature only appeared in bash 4.0. – sam hocevar – 2011-03-20T15:04:21.060

@SamHocevar: I'm running bash 4.2.0. – Wuffers – 2011-03-20T15:05:42.430

@SamHocevar: I've got it, I just needed to make tmux use the right version of bash. – Wuffers – 2011-03-20T15:08:26.653

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 – 2011-04-09T08:41:59.630

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.

user1686

Posted 2011-03-19T15:06:40.560

Reputation: 283 655

That's interesting! In 2007 I wrote a similar [function] (http://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! :)

– user unknown – 2011-03-20T08:57:38.667

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 – 2012-10-11T13:33:39.057

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

artistoex

Posted 2011-03-19T15:06:40.560

Reputation: 3 353

4What's a chdir championship? ignorant – Peter Jaric – 2011-03-19T21:57:41.757

4Directory navigation sports event. – artistoex – 2011-03-19T22:11:03.190

14

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

alias cd..="cd .."

juggler

Posted 2011-03-19T15:06:40.560

Reputation: 1 530

Most inconvenient for a non-unix person occasionally on a *nix box +1 – mplungjan – 2011-03-19T17:52:03.000

11s/non-unix/Windows/ – user1686 – 2011-03-19T19:02:23.310

3If you are typing so fast you are missing characters, it's hard to believe adding alias for cd.. really helps. – Olli – 2011-03-20T12:25:57.323

3I can see how it could help if you come from dear old DOS, where cd.. was valid :) – cambraca – 2011-03-20T19:02:32.617

1For 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. – Syntax Error – 2011-03-20T20:35:37.077

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.

mbrochh

Posted 2011-03-19T15:06:40.560

Reputation: 171

2

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

knittl

Posted 2011-03-19T15:06:40.560

Reputation: 3 452

1

My bashrc file contains

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

as well as

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

Wrashi

Posted 2011-03-19T15:06:40.560

Reputation: 21

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.

FuzzyWuzzy

Posted 2011-03-19T15:06:40.560

Reputation: 256