Introduction
Every Linux user most probably suffers in a situation where we need to jump from one directory to another in a backward direction, multiple times. Conventionally most of us choose to use the ‘..’ parameter with the ‘cd‘ command to denote the previous directory, multiple times. This command might look like this, ‘cd ../../../..‘. This command will go to 4 directories backward. But, this is a tiresome way of doing backward directory navigation, especially when you are a Linux power user. Luckily, we do have a useful utility that can be installed in Linux, to solve this problem. Let’s dive deeper into this utility in this article.
What is bd?
bd is a command line utility in Linux that is used to quickly go back to a specific directory in bash. It is very efficient when it comes to backward directory navigation. This command also comes with features that allow it to be combined with other bash commands for maximum productivity.
How to Install bd?
Open the terminal in your computer and enter the following command to install bd. The command varies for different operating systems and distributions of Linux. You can choose the one that matches your OS.
1. For Debian-based Linux Distro
Execute the below command in the terminal to install the bd command on our Linux system using apt manager.
sudo apt install bd
bd installation in Debian-based Linux
2. For OS X/macOS
Execute the below command in the terminal to install the bd command on our macOS system.
sudo port install bd
3. For Arch Linux
Execute the below command in the terminal to install the bd command on our Arch Linux system.
sudo pacman -S bd
4. For Other Linux Distros
Since we don’t have packages in the repositories of other Linux distributions, you need to follow the below steps to install bd, if this is your case.
Step 1: Download the GitHub project for bd.
wget --no-check-certificate -O /usr/local/bin/bd https://raw.github.com/vigneshwaranr/bd/master/bd
Step 2: Add read and execute permissions to the binary file.
chmod +rx /usr/local/bin/bd
Step 3: Add an alias for bd to the ‘bashrc‘ file
echo 'alias bd=". bd -si"' >> ~/.bashrc
Note: To enable case sensitive directory name matching, use the ‘-s‘ option instead of ‘-si‘
Step 4: Read and execute the content of ‘bashrc‘
source ~/.bashrc
If you need auto-completion support, while using bd, enter the following additional commands in the terminal.
wget -O /etc/bash_completion.d/bd https://raw.github.com/vigneshwaranr/bd/master/bash_completion.d/bd
source /etc/bash_completion.d/bd
To verify if ‘bd‘ is successfully installed on your computer, enter the below command in the terminal.
bd
bd command
If installed successfully, The version and description of ‘bd‘ will get printed on the screen.
How to Check Details of bd Command?
Not only ‘bd‘, but when you install any command line utility which is new to you, you need to seek help, first. There are a few ways to get help regarding how to utilize the ‘bd‘ command.
1. Using Help option
You can use the –help option of the ‘bd‘ command to get help regarding the various options available to be used with the command.
bd --help
Help option in bd
2. Using Manual Page
You could also read the elaborate manual page of the ‘bd‘ command by entering the following command in the terminal.
man bd
The manual page of bd
Apart from the above-listed details, the man page also contains information about various options available, notes about the command, other uses with examples, and contact details of the author of bd.
Usage of bd Command
As discussed before, ‘bd‘ can be used as a stand-alone command or combined with some other bash commands.
1. Stand-alone Usage
‘bd‘ is classically used to instantly go back to a specified directory, in a directory hierarchy. The syntax to perform this action is given below:
Syntax:
bd <target_directory>
Example:
Step 1: Let us consider an example where our present working directory is as follows:
/home/matrix_angel/Public/GFG/Linux/Bash/commands/basic_commands/bd
Step 2: To navigate directly to the ‘GFG‘ directory, we can simply enter the following command.
bd GFG
case-sensitive target directory
Step 3: While providing the alias, if you have given the ‘-si‘ option, then the target name will be case insensitive. This means that the below command will also take you to the ‘GFG‘ directory.
bd gfg
case insensitive target directory
Step 4: It is not even necessary to type the full name of the directory. Instead, you can enter the beginning character of the directory name and use the ‘bd’ command.
bd using the starting character of the directory
In the above example, we have provided only the first character of a directory name in the hierarchy and bd took me to the ‘basic_commands’ directory.
Note: We have two directories in the hierarchy with a name starting with character ‘b‘ (b and B are treated as same due to case insensitivity) – ‘Bash‘ and ‘basic_commands’. In such case, bd takes us to the closest directory backwards which is ‘basic_commands’ , here.
2. Combined Usage
bd gets more powerful when it is used along with other bash commands. To use bd with other commands, we need to enclose the command within backticks. This will make bd to only print the required path but do not take us to that directory. This special feature of bd can be exploited in several creative ways. we haveprovided a few examples below:
Example 1: bd with ls command
ls `bd g`
bd with the ls command
The command ‘bd g‘ will return the path of the ‘GFG‘ directory, which is ‘/home/matrix_angel/Public/GFG‘. This path will be taken as input to the ‘ls‘ command, thus listing out the contents of the ‘GFG‘ directory.
Example 2: bd with mkdir command
mkdir `bd b`/advanced_commands
bd with mkdir command
Instead of typing the absolute path of ‘basic_commands‘ for creating a new directory, by ourselves, we can make use of bd and then append the new directory name at the end.
Example 3: bd with rmdir command
rmdir `bd b`/advanced_commands
bd with rmdir command
Using rmdir with bd is quite similar to using mkdir with bd. In the above example, we have deleted the newly created directory ‘advanced_commands‘ using bd inside backticks.
Example 4: bd with cat command
cat `bd g`/GFG1
bd with cat command
Instead of specifying the absolute path of file ‘GFG1‘ for printing the content of the file, we could make use of bd, here.
Conclusion
The “bd” command is a valuable time-saving tool for command-line users, especially those who frequently work in nested directory structures. Typically, when using the “cd” command to navigate from a subdirectory to its parent directory, you would need to specify the parent directory’s path explicitly. This can become cumbersome and error-prone, particularly when dealing with long or complex directory structures. However, “bd” simplifies this process by allowing users to jump directly to the parent directory with a straightforward “bd” command. This not only reduces the keystrokes required but also minimizes the chances of making typographical errors, making it an efficient and convenient option for directory navigation.
Share your thoughts in the comments
Please Login to comment...