bd – Quickly Go Back to a Parent Directory Instead of Typing “cd ../../..” Redundantly

If You Appreciate What We Do Here On TecMint, You Should Consider:

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Get your own self-hosted blog with a Free Domain at ($3.45/month).
  4. Become a Supporter - Make a contribution via PayPal
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux courses

We are thankful for your never ending support.

Aaron Kili

Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

26 Responses

  1. sandeep says:

    Single quote missing in below command:

    # echo 'alias bd=". bd -si" >> ~/.bashrc
    
    
    
    
  2. Spike says:

    I’ve written a bash function in 5 minutes that does the same, why to install a tool for such trivial things?!

    function bd {
        cd $(pwd | grep -ioP ".*$*.*?/")
    }
    
  3. bac0n says:
    if [ -n "$PS1" ]; then
    
        cd ()
        {
            while [[ "$*" =~ (^|/|\s)[.]{3,}($|/|/s).*$ ]]; do
                set -- "${@//.../..\/..}";
            done;
            builtin pushd "[email protected]" > /dev/null && /bin/ls --almost-all --color=auto
        }
    fi
    
    # cd ... 
    # cd .... 
    # cd .....
    
    • Aaron Kili says:

      @bac0n

      Good bash function, we will take time to analyze it. Thanks a ton for sharing this.

    • Spike says:

      Just made a shell function that should do the same as the presented tool. I think that my solution is prettier than yours. What do you think about this approach?

      function bd {
          cd $(pwd | grep -ioP ".*$*.*?/")
      }
      
      • Bac0n says:

        I think our objective is very different, my function only intents to expand dots. The main challenge was to create it with one loop statement (I probably have to give that up), next was to only use builtins, external commands can be very resource demanding especially in a loop and adds unnecessary dependencies.

        second version… (https://pastebin.com/gCquQjQk)

        • Spike says:

          Ok, but instead of looping I just once call grep on the current path. I would think it’s more efficient than looping, but I’m not sure …

          • bac0n says:
            # time cd ...
            real    0m0,005s
            user    0m0,001s
            sys     0m0,005s
            
            # cd /usr/local/bin/.../bin/.../bin (multiple expansion)
            real    0m0,006s
            user    0m0,002s
            sys     0m0,004s
            
            # time bd usr
            real    0m0,012s
            user    0m0,003s
            sys     0m0,011s
            
  4. Jesse says:

    Why ignore the github.com ssl certificate?

    • Aaron Kili says:

      @Jesse

      That is the command given on the official Github repository for the installation, try to check it.

    • Vigneshwaran says:

      Hi Jesse! I’m the author. Long time back, Github had issues with their certificate so I had to change the instructions to ignore it so wget would be able to download the file.

      You can also just copying the contents file and write to /usr/local/bin/bd using an editor like vim. :)

  5. Jouni "rautamiekka" Järvinen says:

    The backticks are obsoleted by §$(…)§ which supports nesting of commands.

  6. hackel says:

    No Do *not* install user files in /usr/bin! This is terrible advice. Put it in /usr/local/bin (that’s what it’s there for), or add a user dir like ~/.local/bin to your $PATH.

  7. Michael Eager says:

    A simpler version of this, which just steps up one level in the directory tree is:

    alias ..='cd ..;ls'
    

    This creates a command “..” which goes back one directory and does an “ls”. I use it all the time.

  8. Bill says:

    Please don’t suggest using wget –no-check-certificate, that should only be for internal/local use where you can be confident of no MITM attacks.

    Also, when not using your Linux package manager to install a program, you should not be using /usr/bin. For a single user you can install to a folder in your home directory like ~/bin (and add that to your $PATH). Or to make it available to all users of a system you can use /usr/local/bin.

    Never download files as root. Instead of using `sudo wget` you should use wget as a normal user, check the contents of the file and then use sudo to install it to the desired location (here /etc/bash_completion.d/bd).

    Also, there’s no need to use `sudo source` and I’m not sure it would even work. Instead just use `source` as the current user.

    You have responsibilities as a technical writer to give good examples without creating potential security issues for your readers. Please think carefully about commands given in examples.

    • Aaron Kili says:

      @Bill

      Yes, it is always good to share with you guys and receive useful feedback like this. We will consider all you have stressed out here. Many thanks for the heads up.

      • Flurrywinde says:

        Yes, ‘sudo source‘ does not work. Also, you show wget being used to writing the file into /usr/bin/. This needs a sudo (but like Bill says, it shouldn’t be in /usr/bin/ and should be d/led and checked first). Also, looking at the github for bd, it seems it can be installed via a package manager now: https://github.com/vigneshwaranr/bd/issues/32

        Otherwise, thanks for posting this article. `bd` will prove very valuable to me. Thanks!

  9. Raymond Herrera says:

    This command is really cool! I am going to install it right away. This is an excellent opportunity to remind the colleagues out there about a related set of commands: “pushd” and “popd“.

    Thanks and keep up the good work!

Got something to say? Join the discussion.

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.