

Quickly go back to a directory instead of typing cd ../../.. - vigneshwaran
https://github.com/vigneshwaranr/bd
Description: Quickly go back to a specific parent directory in bash instead of typing &quot;cd ..&#x2F;..&#x2F;..&quot; redundantly.<p>How to use:<p>If you are in this path &#x2F;home&#x2F;user&#x2F;project&#x2F;src&#x2F;org&#x2F;main&#x2F;site&#x2F;utils&#x2F;file&#x2F;reader&#x2F;whatever and you want to go to site directory quickly, then just type: &#x27;bd site&#x27;<p>In fact, You can simply type &#x27;bd &lt;starting few letters&gt;&#x27; like &#x27;bd s&#x27; or &#x27;bd si&#x27;<p>If there are more than one directories with same name up in the hierarchy, bd will take you to the closest. (Not considering the immediate parent.)<p>Using bd within backticks (`bd &lt;letter(s)&gt;`) prints out the path without changing the current directory.<p>You can take advantage of that by combining `bd &lt;letter(s)&gt;` with other commands such as ls, ln, echo, zip, tar etc..<p>Check out the screenshot.
======
philfreo
Two other simple tips without requiring a script:

"cd -" will jump back to the last directory you were at, regardless of where
it is.

Add this to your .bashrc / .zshrc / whatever:

alias cdg='cd $(git rev-parse --show-cdup)'

And then type "cdg" to keep going up directories until you reach whichever
folder contains your git repo. Useful when going from
"/Users/philfreo/Projects/myproject/myproject/src/assets/js/views/" back up to
"/Users/philfreo/Projects/myproject/"

~~~
tytso
Note that "git rev-parse --show-cdup" return the empty string if you are at
the top of the git repo. So "cdg" as defined above will drop you in the hope
directory if you are already at the top of your git tree.

~~~
davvid
Yup. Perhaps `git rev-parse --show-toplevel` is better for this alias.

------
tytso
Why not put something like this in your .bashrc or your .profile file instead?

function bd () {

    
    
      OLDPWD=`pwd`
      NEWPWD=`echo $OLDPWD | sed 's|\(.*/'$1'[^/]*/\).*|\1|'`
      index=`echo $NEWPWD | awk '{ print index($1,"/'$1'"); }'`
      if [ $index -eq 0 ] ; then
        echo "No such occurrence."
      else
        echo $NEWPWD
        cd "$NEWPWD"
      fi

}

That way you don't have to execute a separate shell script and source its
output. Also note that the "export OLDPWD" wasn't necessary in your script,
either. Why pollute the environment of future processes spawned by your shell?

~~~
vigneshwaran
Didn't know about the function approach.

Removed the 'export'. I wrote out of habit. Thanks a lot for pointing that
out.

------
logn
This article covers more advanced functionality than what's built in to shells
but don't forget:

    
    
      cd -
    

That goes to previous dir. A utility that follows this convention but adds
some more features would be nice. For instance, maybe typing 'cd -2' could
take me back two spots in history, like a web browser.

~~~
Tmmrn
Sounds like zsh

[http://www.acm.uiuc.edu/workshops/zsh/cd.html](http://www.acm.uiuc.edu/workshops/zsh/cd.html)

~~~
aaronz8
This is exactly what I was going to say, cd +n and cd-n work amazingly.

------
cygni
I have these two aliases in my .zshrc file:

    
    
      alias ..="cd .."
      alias ...="cd ../.."
    

For moving around to other directories I use autojump
([https://github.com/joelthelion/autojump](https://github.com/joelthelion/autojump)).

~~~
kroger
I have the same, but I use cd[a]+:

alias cda='cd ../' alias cdaa='cd ../../' alias cdaaa='cd ../../../'

I guess I was influenced by Lisp at the time, with car, cdr, cdar, cddr, etc
;-)

~~~
Shish2k
> I guess I was influenced by Lisp at the time, with car, cdr, cdar, cddr, etc
> ;-)

You saw that naming scheme and thought "wow, that's sensible"?!

------
B-Con
pushd and popd can be used maintain an easily rewindable history of places you
have been. Use pushd to add the cwd to the stack and cd to another directory.
Use popd to cd to the top dir on the stack and remove it.

    
    
        $ pwd
        /dir1
        $ pushd /some/other/dir
        /some/other/dir /dir1
        $ pwd
        /some/other/dir
        $ popd
        /dir1
        $ pwd
        /dir1

------
nkuttler

        wget -O /usr/bin/bd https:[snip]
        chmod +rx /usr/bin/bd
    

Really? Run wget as root to get a random script, and put it into every user's
PATH and a directory that should only be used by the system? :-|

------
ck2
You know when you are getting old by when you have to start using _history_ a
lot to try to remember what the heck you did.

~~~
dajohnson89
I don't think it means you're getting old, necessarily. I'm well under 30, and
I use the hell out of history. Whenever I need to do something complex, and I
can't figure it out, my solution is often copy/pasted from the web. Bash
history is often the only way I can remember which `sed` switch to use, or
some complex `grep` operation.

In fact, I use history so much, I increased HISTFILESIZE[0] from 1000 commands
to 10000.

[0] [http://osxdaily.com/2012/04/12/change-length-of-bash-
command...](http://osxdaily.com/2012/04/12/change-length-of-bash-command-
history-or-disable-bash-history-completely/)

------
taeric
This has to be one of the first alternatives to a pushd/popd workflow that
actually sounds compelling. Kudos.

~~~
vigneshwaran
Thank you

------
Skunkleton
I have tried stuff like this in the past, but I find it creates too much of a
dependence on special configuration. My keymapping and vimrc are already
enough. Instead, I usually just do a `cd ..` followed by up/enter/up/enter,
which is very easy to type quickly.

------
tobr
An interesting aside: I can't seem to right click that link in Safari (6.0.5).
It seems to be the "../../.." in the tag content that does it, because it
works if I edit it out in the inspector. Does anyone else run in to this?

------
mtdewcmu
I implemented a fairly sophisticated system for navigating directories based
on pushd and popd. I also set up a system based on CDPATH to hop quickly to
directories I go to frequently. I fell out of the habit of using them once my
usage patterns changed, though, and, since I gave "cd -" a new meaning,
sometimes it confuses me. It's hard to come up with an interface that's
intuitive enough to be future-proof.

------
VaucGiaps
alias ..='cd ..'

alias ..1='cd ..'

alias ..2="cd ../.."

alias ..3="cd ../../.."

alias ..4="cd ../../../.."

alias ..5="cd ../../../../.."

~~~
silvestrov

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

~~~
jloughry
I miss the feature of Novell Netware that used to let you type

    
    
      cd ...
    

to go up two levels, or analogously for as many dots as you needed. It was
incredibly useful and fast. Thanks for the tip, and I'm using these aliases
from now on.

------
jvehent
Simple bashrc function

    
    
        # go up X directories
        up_dir() {
            num_of_dirs=$1
            for i in $(seq 1 $num_of_dirs)
            do
                cd ..
            done
        }
        alias up=up_dir
    

Usage:

    
    
        $ pwd
        /home/user/svn/automation/puppet/trunk/modules/ossec/templates
        $ up 4
        $ pwd
        /home/user/svn/automation/puppet

~~~
dylnclrk

        # Hah, I have almost the exact same thing in my bash_profile
        up () {
            TIMES=${1:-1};
    
            for ((i=1; i<=$TIMES; i++));
            do
                cd ..
            done
        }
    

EDIT: Although now I'm looking at it and it looks like I made a strange choice
with `TIMES=${1:-1};`. Oh well, I think it was my first bash script... it's
funny to look back at the hack-y crap that your bash_profile accumulates over
time.

------
typicalbender
I've also found this extremely useful. It lets you mark directories with a
keyword and jump directly to them regardless of where you are.
[http://jeroenjanssens.com/2013/08/16/quickly-navigate-
your-f...](http://jeroenjanssens.com/2013/08/16/quickly-navigate-your-
filesystem-from-the-command-line.html)

------
imurray
Here's my, yet another, alternative to the many nice shell hacks out there: a
bash/zsh "up" command that does "cd ..", or "up <n>" does "cd .." _n_ times.

    
    
        function up () { if test $# = 1 ; then s=$( printf "%$1s" ); s=${s// /..\/}; cd $s ; else cd .. ; fi; }

------
DiabloD3
So why don't you just use jump? A lot of people already use it and you're not
limited to just directories parenting yours.

~~~
vigneshwaran
Hi, 'jump' is a great bookmarking system for the bash shell. But you'd only
want to bookmark some most used directories.

I made 'bd' out of frustration to use 'cd ../..' all the time from "any"
random directory. I can't bookmark all possible places. They are like slightly
different tools.

I think it'll be awesome to use both jump and bd in the same system!

Please give 'bd' a try. Once you get used to 'bd', you'll use it frequently
without thinking. :)

~~~
hk__2
You can use z, then. You don’t have to bookmark directories to jump to them.

~~~
vigneshwaran
Thanks. z is great! If only I had found this before. I actually googled a lot
before writing this (last year April) but couldn't find something like z.

Anyways z always changes directory. Using bd within backticks will just print
the path without changing the directory. We can use that with other commands
like ls, zip etc. Example: "ls `bd <starting letters>`" would only print the
contents without changing the directory.

~~~
arjunrc
An obligatory addition - check out fasd
([https://github.com/clvv/fasd](https://github.com/clvv/fasd)), inspired by z.

In addition to "z foldername" to move around, you can do things like "v vimrc"
or "v freetds.conf" to directly edit files, where ever you are.

It's available on homebrew.

------
jmulder
Very useful! Thank you ver much! Although I'd like to add one suggestion and
that is to surpress an 'echo' on successful navigation. I'd see my path twice
right underneath of eachother, which just clutters up the screen.

------
wicknicks
Smart idea. Good job! Could you extend bd to jump to bookmarked directories. I
often jump between /data... and ~/dir/containing/code/ \-- would be useful to
have one command help with both scenarios.

~~~
vigneshwaran
Somebody commented that there is a nice tool called 'jump' for bookmarking
directories. [https://github.com/flavio/jump](https://github.com/flavio/jump)

You can use both 'jump' to jump between bookmarked directories and 'bd' to
quickly go to a parent directory in the same shell.

------
helpermethod
Shameless plug: This works similar to a script called 'up' which I am the
author of:
[https://github.com/helpermethod/up](https://github.com/helpermethod/up)

------
MetaCosm
For those using ZSH: [http://stackoverflow.com/questions/3986760/cd-1-2-3-etc-
in-z...](http://stackoverflow.com/questions/3986760/cd-1-2-3-etc-in-z-shell)

------
Tarrasch
For zsh users, I created a similar command with auto-completion! No root
access needed to install!

[https://github.com/Tarrasch/zsh-bd](https://github.com/Tarrasch/zsh-bd)

------
paulkroka
How about using

bind -x '"\C-h":"cd ../;ls"'

(putting it into .bashrc) and then using control-h as a command on the shell
to move a directory upwards. very useful for me.

~~~
vigneshwaran
That's a good one!

------
ballard
zsh can be set to automagically track directories (like using `pushd` for you
when using `cd`), so all that's needed is `popd`. No scripting required.

------
D9u
In FreeBSD I use the following to change to an arbitrary directory when I'm
unsure of the correct path:

`whereis -sq <dirname>`

No extra software necessary.

------
jostmey
Why not "cd -" ?

~~~
onedognight
"cd -" only supports the directory you most recently came from ($OLDPWD),
while "bd" supports any directory that is a prefix of $PWD.

