I’ve run across more than one fast directory switcher in the past 15 months, most notably fasd and j and j2. What you might not know is that the bash shell comes with a primitive fast directory switcher … of a sort.
popd
and pushd
are built-in commands in bash (and zsh, I believe) that work with a manageable stack of directories that you can bounce between. And since it’s been a while since we actually dug in to one of these tools, it’s probably a good time for an in-depth look at pushd
and popd
.
Compared with fasd et al., pushd
/popd
may take a little getting used to. Here’s a simple example:
kmandla@lv-r1fz6: ~$ pushd /usr/share/
/usr/share ~
kmandla@lv-r1fz6: /usr/share$
When I use pushd
and a directory, I bounce straight to that location, and the path is put into the directory stack. pushd
by itself effectively swaps entry Nos. 0 and 1. Either way, pushd
‘s output is the current directory stack, from left to right, zero on up.
I can check what’s in the stack and the order with dirs
(I like dirs -v
, personally).
kmandla@lv-r1fz6: /usr/share$ dirs -v
0 /usr/share
1 ~
kmandla@lv-r1fz6: /usr/share$
The stack works LIFO, meaning last-in-first-out: The last (or newest) item added to the the stack is the first one used.
Zero (or the leftmost entry) is your current directory; you can cd
to a new location without interfering with the stack. I can also add another destination if I like.
kmandla@lv-r1fz6: /usr/share$ pushd /lib/kernel
/lib/kernel /usr/share ~
kmandla@lv-r1fz6: /lib/kernel$
Now I have three in the stack. If I want to bounce back to a previous directory, I use popd
to “pop” out the zero entry and jump to No. 1 in the stack.
kmandla@lv-r1fz6: /lib/kernel$ popd
/usr/share ~
kmandla@lv-r1fz6: /usr/share$
Note that I jumped back to /usr/share, and that /lib/kernel was removed from the list. The whole stack shuffles up one space.
If I had a lot of directories in the stack, I could successively popd
through them until I arrived back where I began, and the stack would be empty.
kmandla@lv-r1fz6: /home$ dirs -v
0 /home
1 /boot
2 /lib/kernel
3 /usr/share
4 ~
kmandla@lv-r1fz6: /home$ popd
/boot /lib/kernel /usr/share ~
kmandla@lv-r1fz6: /boot$ popd
/lib/kernel /usr/share ~
kmandla@lv-r1fz6: /lib/kernel$ popd
/usr/share ~
kmandla@lv-r1fz6: /usr/share$ popd
~
kmandla@lv-r1fz6: ~$
It’s not ideal if you have a series of directories you want to keep, and it’s hard to imagine how it works if you’re trying to visualize it like “bookmarks.” It’s not really like that at all.
In fact, it might be easier to think of it like a stack of plates: You can “push” a plate onto the top of the stack, and then “pop” out whatever is on top.
And aside from pushing and popping, there are some rudimentary management tricks. You can use dirs -c
to clear the stack, -l
lists them longhand and -p
prints them in plain form. Using a plus or minus displays the number of entries from the left or right in the stack, respectively. Remember again that the leftmost is No. 0, and they increment as you move to the right.
popd
and pushd
work differently with the plus or minus; popd
“pops” out the Nth entry from the left with minus, and from the right with a plus. pushd
rotates the stack a certain number of steps to the left or right, with plus or minus.
kmandla@lv-r1fz6: /home$ dirs -v
0 /home
1 /boot
2 /lib/kernel
3 /usr/share
4 ~
kmandla@lv-r1fz6: /home$ pushd +1
/boot /lib/kernel /usr/share ~ /home
kmandla@lv-r1fz6: /boot$ pushd +2
/usr/share ~ /home /boot /lib/kernel
kmandla@lv-r1fz6: /usr/share$ pushd -2
/home /boot /lib/kernel /usr/share ~
kmandla@lv-r1fz6: /home$ pushd -1
/usr/share ~ /home /boot /lib/kernel
kmandla@lv-r1fz6: /usr/share$ dirs -v
0 /usr/share
1 ~
2 /home
3 /boot
4 /lib/kernel
kmandla@lv-r1fz6: /usr/share$
Note that each time, the list was shuffled and the current working directory changed.
Final tip: If it annoys you that popd
always dumps an entry off your stack, start relying on pushd +N
, which will just cycle you through the list. And remember: pushd
by itself just swaps Nos. 0 and 1 in the stack, effectively bouncing you between two directories. š Aha! You can thank me later. š
“Modern” fast directory switchers might have more conventional features, but for sheer lightness and convenience, pushd
, popd
and dirs
are reliable and useful. Not bad, for software that dates back to 1976. šÆ
If you want more guidance, try the bash reference manual, or enter help
with any of pushd
, popd
or dirs
. The learning curve is not too steep, but it may take some getting used to.
Enjoy. š