Bd – Quickly Go Back to a Parent Directory Instead of Typing


Have you ever found yourself stuck in a deep nested directory structure while using command line interface and wished there was a faster way to navigate back to a parent directory instead of repeatedly typing "cd ../../.."? Well, you're in luck because there is a simple solution that can save you time and reduce chances of making errors in your commands. In this article, we'll introduce you to "bd" command and show you how it can make your life easier.

What is Bd?

"bd" stands for "back directory" and is a command-line tool that allows you to quickly navigate back to a parent directory without having to type out full path. It works by searching through your directory history to find most recent parent directory and then automatically changing your current working directory to that directory.

How to Install Bd?

Installing "bd" is easy and can be done using your favorite package manager. For example, if you're using a Debian-based Linux distribution, you can install "bd" by running following command −

sudo apt-get install autojump

If you're using a Mac with Homebrew, you can install "bd" by running −

brew install autojump

Once you've installed "bd", you're ready to start using it.

How to Use Bd?

Using "bd" is straightforward. To go back to parent directory, simply type "bd" followed by Enter key. For example, if you're in following directory −

/Users/johndoe/Documents/projects/projectA/src/components/Button

You can go back to "src" directory by typing −

bd

And pressing Enter key. "bd" will automatically take you to most recent parent directory, which in this case is "src".

If you want to go back multiple directories at once, you can specify number of levels to go back. For example, if you're in following directory −

/Users/johndoe/Documents/projects/projectA/src/components/Button/tests

You can go back two directories by typing −

bd ..

And pressing Enter key. "bd" will take you back two levels to "src" directory.

You can also use tab completion to quickly navigate to a directory. For example, if you're in following directory −

/Users/johndoe/Documents/projects/projectA/src/components/Button

And you want to navigate to "src" directory, you can type −

cd src

And then press Tab key. command line interface will automatically complete directory name for you. You can then press Enter to navigate to directory.

How Does Bd Work?

"bd" works by using a database to keep track of your directory history. Every time you change directories, "bd" records path to directory in database. When you type "bd", it searches through database to find most recent parent directory and then changes your current working directory to that directory.

The database used by "bd" is similar to one used by "autojump" command. If you've used "autojump" before, you'll be familiar with way "bd" works.

Why Use Bd?

Using "bd" can save you time and reduce chances of making errors in your commands. Instead of typing out full path to a parent directory, you can simply type "bd" and let command do work for you.

"bd" can also be used in scripts to navigate back to a parent directory. For example, if you're writing a script that needs to navigate to a directory and then back to parent directory, you can use "bd" instead of typing out full path of parent directory. This can make your scripts more readable and easier to maintain.

In addition, "bd" is customizable. You can change behavior of "bd" by modifying its configuration file. For example, you can change key combination used to invoke "bd" or modify database used by "bd".

Examples of Bd Usage

Let's take a look at some examples of how "bd" can be used in practice.

Example 1: Navigating to a Parent Directory

Suppose you're in following directory −

/Users/johndoe/Documents/projects/projectA/src/components/Button/tests/unit

And you want to navigate to "tests" directory. Instead of typing out full path, you can simply type −

bd

And press Enter. "bd" will take you back to "tests" directory.

Example 2: Navigating Multiple Levels Up

Suppose you're in following directory −

/Users/johndoe/Documents/projects/projectA/src/components/Button/tests/unit

And you want to navigate to "src" directory. Instead of typing out full path, you can simply type −

bd ..

And press Enter twice. "bd" will take you back two levels to "src" directory.

Example 3: Using Tab Completion

Suppose you're in following directory −

/Users/johndoe/Documents/projects/projectA/src/components/Button

And you want to navigate to "src" directory. Instead of typing out full path, you can simply type −

cd src

And press Tab key. command line interface will automatically complete directory name for you. You can then press Enter to navigate to directory.

Here are some additional tips and tricks that you can use when working with "bd" −

Use "bd -" to go Back to Previous Directory

If you want to quickly switch back and forth between two directories, you can use "bd -". This command takes you to previous directory, which is directory you were in before you switched to current directory.

For example, if you're in following directory −

/Users/johndoe/Documents/projects/projectA/src/components/Button

And you want to quickly switch back to "tests" directory, you can type −

bd -

And press Enter. "bd" will take you back to "tests" directory.

Use "bd -l" to List Directory History

If you want to see a list of directories that are stored in "bd" database, you can use "bd -l". This command lists directories in order that they were visited.

For example, if you've visited following directories −

/Users/johndoe/Documents/projects/projectA/src/components/Button
/Users/johndoe/Documents/projects/projectA
/Users/johndoe/Documents

You can type −

bd -l

And press Enter. "bd" will list directories in following order −

/Users/johndoe/Documents/projects/projectA/src/components/Button
/Users/johndoe/Documents/projects/projectA
/Users/johndoe/Documents

Use "bd -i" to Interactively Choose a Directory

If you don't remember exact name or location of parent directory you want to navigate to, you can use "bd -i". This command allows you to interactively choose a directory from a list of directories that match a given pattern.

For example, if you want to navigate to a directory that contains word "projects" in its name, you can type −

bd -i projects

And press Enter. "bd" will list all directories that contain word "projects" in their name. You can then choose directory you want to navigate to by typing its number and pressing Enter.

Use "bd -s" to Search for a Directory

If you know name or a part of name of directory you want to navigate to, you can use "bd -s". This command searches through directory history for directories that match a given pattern and then takes you to most recent directory that matches pattern.

For example, if you want to navigate to a directory that contains word "components" in its name, you can type −

bd -s components

And press Enter. "bd" will search through directory history for directories that contain word "components" in their name and take you to most recent directory that matches pattern.

Conclusion

In this article, we've introduced you to "bd" command, which allows you to quickly navigate back to a parent directory without having to type out full path. We've shown you how to install and use "bd", and explained how it works.

Using "bd" can save you time and reduce chances of making errors in your commands. It's also customizable, which means you can change its behavior to suit your needs.

So next time you find yourself stuck in a deep nested directory structure, give "bd" a try and see how it can make your life easier.

Updated on: 10-Apr-2023

455 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements