Hack 5. Use dirs, pushd and popd to manipulate directory stack

You can use directory stack to push directories into it and later pop directory from the stack. Following three commands are used in this example.

  • dirs: Display the directory stack
  • pushd: Push directory into the stack
  • popd: Pop directory from the stack and cd to it


Dirs will always print the current directory followed by the content of the stack. Even when the directory stack is empty, dirs command will still print only the current directory as shown below.

# popd
-bash: popd: directory stack empty

# dirs
~

# pwd
/home/ramesh

How to use pushd and popd? Let us first create some temporary directories and push them to the directory stack as shown below.

# mkdir /tmp/dir1
# mkdir /tmp/dir2
# mkdir /tmp/dir3
# mkdir /tmp/dir4

# cd /tmp/dir1
# pushd .

# cd /tmp/dir2
# pushd .

# cd /tmp/dir3
# pushd .

# cd /tmp/dir4
# pushd .

# dirs
/tmp/dir4 /tmp/dir4 /tmp/dir3 /tmp/dir2 /tmp/dir1

[Note: The first directory (/tmp/dir4) of the dir command output is always the current directory and not the content from the stack.]

At this stage, the directory stack contains the following directories:

/tmp/dir4
/tmp/dir3
/tmp/dir2
/tmp/dir1

The last directory that was pushed to the stack will be at the top. When you perform popd, it will cd to the top directory entry in the stack and remove it from the stack. As shown above, the last directory that was pushed into the stack is /tmp/dir4. So, when we do a popd, it will cd to the /tmp/dir4 and remove it from the directory stack as shown below.

# popd
# pwd
/tmp/dir4

[Note: After the above popd, directory Stack Contains:
/tmp/dir3
/tmp/dir2
/tmp/dir1]

# popd
# pwd
/tmp/dir3

[Note: After the above popd, directory Stack Contains:
/tmp/dir2
/tmp/dir1]

# popd
# pwd
/tmp/dir2

[Note: After the above popd, directory Stack Contains: /tmp/dir1]

# popd
# pwd
/tmp/dir1

[Note: After the above popd, directory Stack is empty!]

# popd
-bash: popd: directory stack empty

Comments on this entry are closed.

  • nilnull February 7, 2011, 12:15 pm

    Why doing
    # cd /tmp/dir1
    # pushd .

    instead of simply
    # pushd /tmp/dir1

  • Asmita Thapliyal February 8, 2014, 2:51 am

    Can you please explain what is the use of this directories stack? How can we use this stack in daily linux administrations work?

  • BZT March 21, 2014, 9:09 pm

    Asmita:

    Think of it like the “back” button of a Web browser. It’s a means of navigating between directories in a terminal using fewer keystrokes than usual. And the “stack” remembers path names better than any person could, so you can also review where you’ve been (near analogy: the ‘History’ menu of a Web browser). The command ‘dirs -v’ in BASH 4 and later arranges the directory names vertically and by number; a cd command with a tilde and the number of the directory to which you want to go (ie, cd ~4) should get you there, much like selecting a line in your Web browser’s History menu would. Where it falls down is if there are spaces in the path names (as with GVFS file sharing in Gnome 2), And unlike a Web browser’s History, it only records changes in working directories from when you start your terminal emulator. Exit the shell, and it forgets them. (At least that’s been my experience.)

    BZT