On occasion, you may be working in one directory, and then you have to perform some quick task in another, intending to return to the current directory. To do this, you could use:
{ecelinux:5} cd ~/public_html {ecelinux:6} gvim index.html # ...do something... {ecelinux:7} cd ~/ece250/p1 #return and keep working
Worse, you may have to jump between two directories in which you are working. You could use history and keep repeating the two commands using !n, however this is unnecessarily tedious.
Unix provides a quick way to perform these operations through a stack which is accessed and manipulated by three commands: pushd, popd, and dirs (push directory, pop directory, and directory stack).
The top of the stack always contains the current directory. Changing the current directory changes the top of the stack.
{ecelinux:1} dirs ~/public_html/Unix/jumping {ecelinux:2}
When you issue the command pushd directory then the directory directory is pushed onto the stack and the current directory is switched to directory. For example,
{ecelinux:2} pushd ~/projects/p1 ~/projects/p1 ~/public_html/Unix/jumping {ecelinux:3} cd .. # go up one directory {ecelinux:4} dirs ~/projects ~/public_html/Unix/jumping {ecelinux:5}
Note that dirs prints the highest entry of the stack first.
Each time you call pushd with another directory, the new directory is pushed onto the stack.
The command pushd by itself swaps the top two entries on the stack and changes to the working directory to whatever was in the second place of the stack. This allows you to jump quickly between two directories.
{ecelinux:5} dirs ~/projects ~/public_html/Unix/jumping {ecelinux:6} pushd ~/public_html/Unix/jumping ~/projects {ecelinux:7} pwd /home/ece250/public_html/Unix/jumping {ecelinux:8}
The command popd pops the top entry off of the stack and changes the directory to the remaining entry on the stack. If the stack has only one entry, an error is issued:
{ecelinux:8} dirs ~/public_html/Unix/jumping ~/projects {ecelinux:9} popd ~/projects {ecelinux:10} pwd /home/ece250/projects {ecelinux:11} dirs ~/projects {ecelinux:12} popd popd: Directory stack empty. {ecelinux:13}
The commands pushd and popd also allow you to jump within the stack with an option, however, you may issue man pushd to find out more.
Copyright ©2005-2008 by Douglas Wilhelm Harder. All rights reserved.