Back command in linux

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

While navigating the file system via the command line on Linux systems, in order to move back into a parent directory (in a long path), we would normally issue the cd command repeatedly ( cd ../../.. ) until we land in the directory of interest.

This can be so tedious and boring much of the time, especially for experienced Linux users or system administrators who carry out so many various tasks, and therefore hope to discover shortcuts to ease their jobs while operating a system.

In this article, we will review a simple but helpful utility for quickly moving back into a parent directory in Linux with the help of the bd tool.

bd is a handy utility for navigating the filesystem, it enables you to quickly go back to a parent directory without typing cd ../../.. repeatedly. You can reliably combine it with other Linux commands to perform a few daily operations.

How to Install bd in Linux Systems

On Debian-based and Arch Linux distributions, you can install bd from the default repositories using your package manager as shown.

$ sudo apt install bd [On Debian, Ubuntu and Mint] $ sudo pacman -S bd [On Arch Linux]

On other distributions, run the following commands to download and install bd under /usr/bin/ using the wget command, make it executable and create the required alias in your ~/.bashrc file:

$ wget --no-check-certificate -O /usr/local/bin/bd https://raw.github.com/vigneshwaranr/bd/master/bd $ chmod +rx /usr/local/bin/bd $ echo 'alias bd=". bd -si"' >> ~/.bashrc $ source ~/.bashrc

Note: To enable case-sensitive directory name matching, set the -s flag instead of -si in the alias created above.

To enable autocomplete support, run these commands:

$ sudo wget -O /etc/bash_completion.d/bd https://raw.github.com/vigneshwaranr/bd/master/bash_completion.d/bd $ sudo source /etc/bash_completion.d/bd

How to Use bd in Linux Systems

Assuming you are currently in the following long directory path:

/media/aaronkilik/Data/Computer Science/Documents/Books/LEARN/Linux/Books/server

and you want to go to the Documents directory quickly, then simply type:

Then to go straight into the Data directory, you can type:

Switch Between Directories Quickly

Actually, bd makes it even more straightforward, all you need to do is just type bd such as:

Quickly Switch Directories

Important: In case there is more than one directory with the same name up in the hierarchy, bd will move you into the closest without considering the immediate parent as explained in the example below.

For instance, in the path above, there are two directories with the same name Books, if you want to move into:

/media/aaronkilik/Data/ComputerScience/Documents/Books/LEARN/Linux/Books

Typing bd books will take you into:

/media/aaronkilik/Data/ComputerScience/Documents/Books

Move to

Additionally, using bd within backticks in the form `bd ` prints out the path minus changing the current directory, so you can use `bd ` with other common Linux commands such as ls, echo, etc.

Читайте также:  Aarch64 linux gnu toolchain

In the example below, am currently in the directory, /var/www/html/internship/assets/filetree and to print the absolute path, long-list the contents and sum up the size of all files in the directory html without moving into it, I can just type:

$ echo `bd ht` $ ls -l `bd ht` $ du -cs `bd ht`

Switch Directory with Listing

Find out more about the bd tool on Github: https://github.com/vigneshwaranr/bd

That’s all! In this article, we showed reviewed a handy way of quickly navigating the filesystem in Linux using the bd utility.

Have your say via the feedback form below. Plus, do you know of any similar utilities out there, let us know in the comments as well.

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.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Parted Command in Linux

TLDR Man Pages for Linux Commands

apt-get Command Examples

Ubuntu apt-cache Commands

apt Command Examples

Funny Linux Commands

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

# echo 'alias bd=". bd -si" >> ~/.bashrc

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

if [ -n "$PS1" ]; then cd () < while [[ "$*" =~ (^|/|\s)[.]($|/|/s).*$ ]]; do set -- "$"; done; builtin pushd "[email protected]" > /dev/null && /bin/ls --almost-all --color=auto > fi # cd . # cd . # cd .

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?

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) Reply

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 … Reply

# 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

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

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

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

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

Читайте также:  How to close port in linux

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

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

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! Reply

@Flurrywinde Okay, many thanks for writing back, point taken. We’ll consider these important issues in future articles. Reply

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! Reply

@Raymond Many thanks for appreciating our work, and sharing these two useful command which we’ll review as soon as possible. Reply

Источник

Go back to previous directory in shell

Is there a way to go back to previous directory we were in using bash,tcsh without using pushd/popd ? I’d like to type something like «back» and got returned to the previous directory I was in. Edit: «cd -» works, but only for current and previous directories. Is there anyway I can go back to the previous previous previous directory like how you can go back in the web browser? Regards

Best answer imho : unix.stackexchange.com/a/180640/158177 provides cd -1 to cd -9 which I think is what the OP asked for

@sdaffa23fdsf cd — is equivalent to cd . No need to type the — unless you want to separate a list of paths from a list of options to avoid ambiguity. In your case, there’s no such list, so the — is redundant. cd on its own changes to the home directory.

Читайте также:  Bin исполняемый файл linux

4 Answers 4

cd — (goes back to previous directory)

If you want to be able to go to the other previous directories, this is not possible out of the box. But check this script and instructions:

The cd command works as usual. The new feature is the history of the last 10 directories and the cd command expanded to display and access it. cd — (or simply pressing ctrl+w) shows the history. In front of every directory name you see a number. cd -num with the number you want jumps to the corresponding directory from the history.

@ogc-nick no it doesn’t. The — simply separates a command and its options from the parameters (see this post). Because no arguments follow after — , the final command is just cd which switches to your home directory. That might have been the second previous directory, but that’s just a coincidence.

You can also use variable cd $OLDPWD . This can be also used in shell scripts.

$OLDPWD maintains the last directory you came from which is good for scripts. I use $OLDPWD with cp command a lot. E.g cp -v $OLDPWD/file .

It is worth pointing out that using cd $OLDPWD does not print anything to standard output, while cd — seems to usually do. This is a better solution for most scripts.

I dont think so. The cd — comes handy when you work in interactive shell so you do not have to write long commands and have feedback where you are. But for scripts you should definitelly use $OLDPWD as scripts usually do not want cd to print anything to stdout! Also the value of $OLDPWD does not need to be used just for going back. You can for example use it with ls or compare to $HOME or other directory. The POSIX says that cd — shall be equivalent of cd «$OLDPWD» && pwd .

I find the easiest way to do it is with this .bashrc power edit: https://github.com/wting/autojump . You get to «mark» folders you navigate to, giving them a shorthand name that’s easy to remember (my advice; the foregoing is not in the docs), such as Pics for Pictures, etc. ‘jump’ returns you to the folder you ‘marked,’ and ‘marks’ lists folders you have added to the ‘stack’ (as with pushd and popd), with the added advantage that your marks remain the same from one session to the next, ad infinitum.

I have yet to try it on more than one harddrive, but the results should be similar to those using a single volume.

I think cd .. might help. If you do a ls -a in any directory you would see that there are two entries: one named «.» and another named «..»; the single dot is reference to the directory you are already in, while the double is the previous directory in the path.

This answer provides useful information even if it does not answer correctly the question. Therefore there is no point in piling negative votes on it, I upvoted for the effort.

Источник

Оцените статью
Adblock
detector