Pushd and popd are the fastest navigation commands you’ve never heard of.

The original address: opensource.com/article/19/…

The original author: opensource.com/users/seth

Release time: August 07, 2019

Photo by Opensource.com

The pushd and popd commands are built-in features of the Bash shell to help you “bookmark” directories for quick navigation between locations on your hard drive. You may already feel that terminals are an impossible way to browse your computer quickly; With a few keystrokes, you can go anywhere on your hard drive, attached storage or network share. However, this speed is broken when you find yourself moving back and forth between directories or “getting lost” in the file system. These are the problems that PusHD and Popd can help you solve.

pushd

At its most basic, PUSHD is very similar to CD. It takes you from one directory to another. Suppose you have a directory called 1, which contains a subdirectory called 2, which contains a subdirectory called 3, and so on. If your current working directory is one, then you can use the CD command to move to two, three, or anywhere.

$ pwd
one
$ cd two/three
$ pwd
three
Copy the code

You can also do it with pushd.

$ pwd
one
$ pushd two/three
~/one/two/three ~/one
$ pwd
three
Copy the code

The end result of pusHD is the same as CD, but with an additional intermediate result: Pushd will trace back to your destination directory and your origin. This is your directory stack and what makes PusHD unique.

The stack

A stack, in computer terms, is a collection of elements. In the context of this command, these elements are the directories you recently accessed using the pushd command. Think of it as a historical record or breadcrumb clue.

You can use the pushd command to move through the file system; Each time you move, your previous and new positions are added to the stack.

$ pushd four
~/one/two/three/four ~/one/two/three ~/one
$ pushd five
~/one/two/three/four/five ~/one/two/three/four ~/one/two/three ~/one
Copy the code

The stack of navigation

Once you’ve built a stack, you can use it as a bookmark or a collection of waypoints for quick trips. For example, suppose that in a session you do a lot of work in this example’s ~/one/two/three/four/five directory structure. You know you’ve been to one recently, but you can’t remember where it is in your pushd stack. You can view your stack with the +0(that is, a plus sign followed by a zero) argument, which tells pusHD not to change to any directory in your stack and prompts pusHD to echo your current stack.

$ pushd +0
~/one/two/three/four ~/one/two/three ~/one ~/one/two/three/four/five
Copy the code

Alternatively, you can view the stack with the dirs command and see the index number of each directory by using the -v option.

$ dirs -v
0  ~/one/two/three/four
1  ~/one/two/three
2  ~/one
3  ~/one/two/three/four/five
Copy the code

The first item on the stack is your current location. You can confirm with PWD as usual.

$ pwd
~/one/two/three/four
Copy the code

Starting at 0 (your current location and the first item on the stack), the second element in the stack is ~/one, which is the destination you want. You can use the +2 option to move forward in your stack.

$ pushd +2
~/one ~/one/two/three/four/five ~/one/two/three/four ~/one/two/three
$ pwd
~/one
Copy the code

This changes your working directory to ~/one and moves the stack so that your new location is in front.

You can also move backwards in your stack. For example, to get to ~/one/two/three quickly, you can move one back in the given sample output, remembering that pushd starts at 0.

$ pushd -0
~/one/two/three ~/one ~/one/two/three/four/five ~/one/two/three/four
Copy the code

Add to stack

You can continue browsing your stack in this way, and it will keep a static list of your most recently visited directories. If you want to add a directory, simply provide the directory’s path. If a directory is new on the stack, it will be added to the list as you would expect.

$ pushd /tmp
/tmp ~/one/two/three ~/one ~/one/two/three/four/five ~/one/two/three/four
Copy the code

But if it is already on the stack, it is added again.

$ pushd ~/one
~/one /tmp ~/one/two/three ~/one ~/one/two/three/four/five ~/one/two/three/
Copy the code

While the stack is often used as a list of directories you want to access quickly, it’s actually the real history of where you’ve been. If you do not want a directory to be added to the stack unnecessarily, you must use the +N and -n symbols.

Removes directories from the stack

Obviously, your stack is not immutable. You can add items to the stack with pusHD or remove items from the stack with popD.

For example, suppose you just added ~/one to your stack with pushd, making ~/one your current working directory. Remove the first element (or “Zeroeth”, if you prefer).

$ pwd
~/one
$ popd +0
/tmp ~/one/two/three ~/one ~/one/two/three/four/five ~/one/two/three/four
$ pwd
~/one
Copy the code

Of course, you can delete any element and count from zero.

$ pwd ~/one
$ popd +2
/tmp ~/one/two/three ~/one/two/three/four/five ~/one/two/three/four
$ pwd ~/one
Copy the code

You can also use popd to start at the end of your stack, again starting at 0. For example, to remove the last directory from your stack.

$ popd -0
/tmp ~/one/two/three ~/one/two/three/four/five
Copy the code

When used in this way, POPd does not change your working directory. It only operates on your stack.

Using popd navigation

The default behavior of POPD, with no arguments, is to remove the first (zero) item from the stack and make the next item the current working directory.

This is the most useful quick change command when you’re working in two different directories, for example, and you just need to temporarily hide somewhere else. If you don’t need a detailed history, you don’t have to worry about your directory stack.

$ pwd
~/one
$ pushd ~/one/two/three/four/five
$ popd
$ pwd
~/one
Copy the code

You also don’t need to use pusHD and POPD in quick succession. If you use pusHD to access different locations and then get distracted for three hours chasing down a bug or doing research, you’ll find your directory stack waiting patiently (unless you’ve already ended the terminal session).

$ pwd ~/one
$ pushd /tmp
$ cd {/etc,/var,/usr}; sleep 2001
[...]
$ popd
$ pwd
~/one
Copy the code

PUSHD and POPD in the real world

The pushd and popd commands are surprisingly useful. Once you learn them, you’ll find excuses to use them well, and you’ll become familiar with the concept of a directory stack. Familiarity with pushd is what helps me understand git Stash, which is completely unrelated to pushd but has a similar immateriality conceptually.

It may be tempting to use pushd and popd in shell scripts, but in general, it’s best to avoid them. They are not portable outside of Bash and Zsh, and they can be obscure when you re-read a script (pushd +3 is less clear than CD HOME /home/dir/$TMP or similar commands).

In addition to these caveats, if you are a regular Bash or Zsh user, then you can and should try pushd and popd.

Opensource.com/article/17/…


www.deepl.com translation