brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: pushd and popd give your shell a stack of folders to jump back to

Shell
~/tips/tip-shell-pushd-popd-dirs

pushd /var/log

Keeps a numbered stack of folders so you can hop back and forth between them.

cd - only remembers one folder. When I work in three places at once, I would rather have a proper stack.

pushd changes directory and remembers where you were, popd goes back, and dirs -v lists the stack with index numbers:

pushd /var/log
pushd /etc
dirs -v
popd

Each pushd puts a new entry on top and makes it the current directory. The manual says the current directory is always the first entry of the stack. After the commands above, dirs -v shows /etc at index 0, /var/log at 1 and the folder you started in at 2. popd then returns you to /var/log.

The numbers let you jump around inside the stack. pushd +1 rotates it so the entry at index 1 becomes the top, and with it your current directory. popd +1 removes that entry from the stack without moving you, and popd -n only changes the stack and does not change directory. Without arguments, pushd swaps the top two entries, which makes it work like cd - between two folders.

pushd +1
dirs -c

The second line clears the whole stack.

The stack lives in one shell session, so a new terminal tab starts with an empty one. Both commands also print the stack after every change, which is fine at the prompt and noisy in a script. Silence it with pushd /path >/dev/null.

Further reading: bash(1) manual page

More Shell tips zmv -n '(*).log' '$1.txt' zoxide query -ls project f=$(mktemp --suffix=.json) shopt globskipdots echo $EPOCHSECONDS All Shell tips (78) All topics

Link