Navigation
The dirs command
Shows the directory stack managed by pushd and popd. The top entry is the current directory and the entries below are the places you can return to.
Most useful options
-vNumbers each stack entry, one per line-cEmpties the stack. It does not change the current directory-pShows the stack with one entry per line, without numbers+N -NShows entry N counting from the top (+N) or from the bottom (-N)Copy-ready examples
$ pushd ~/proyectosenters proyectos and saves the previous directory on the stack. pushd prints the stack.Example output
~/proyectos ~
$ pushd /var/loganother jump. The stack now has three entries.Example output
/var/log ~/proyectos ~
$ dirs -vlists the numbered stack, with the top at position 0.Example output
0 /var/log 1 ~/proyectos 2 ~
$ popdremoves /var/log from the stack and takes you back to ~/proyectos.Example output
~/proyectos ~
$ dirs -cclears the stack. The current directory stays the same.You can try them in the browser terminal: it does not touch your computer.
Common errors
| Message | Cause | Fix |
|---|---|---|
| dirs: usage: dirs [-clpv] [+N] [-N] | You wrote an option that dirs does not know. | Check the valid options with help dirs. |
| bash: dirs: -x: invalid number | It only accepts -c, -l, -p, -v and numbers preceded by + or -. Any other letter with a dash is rejected. | Use one of the options in the list. |
| bash: dirs: directory stack empty | You ask for an entry beyond the end of the stack. | Check how many entries there are with dirs -v and use a smaller number. |
Typical mistakes
- The stack is lost when the terminal closes. It is not a permanent history.
- pushd prints the stack every time it is used. In scripts, silence the output with > /dev/null if you do not want it.
- cd does not add anything to the stack. If you change directory with cd, dirs does not record it.
- Paths inside your home folder are shortened with ~, so you do not always see the full path.
Differences from similar commands
FAQ
How do I go back to the previous directory?
With popd, which removes the top of the stack and takes you to the directory you were in before. Repeat popd to keep walking back through the stack.
What is entry 0 in dirs -v?
It is the directory you are in right now, the top of the stack.
Why is the stack empty in another terminal?
Each terminal has its own stack. It is not shared between windows and it is not saved when closing.
More Navigation commands
Learn to actually use it
The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.
Start the free course