Chapter 5. Additional Bash Built-in Commands
55. dirs, pushd, and popd
Use dirs, pushd and popd to manipulate the bash directory stack. You can use the directory stack to save and restore your current working directory by pushing and popping, respectively.
• dirs: Display the directory stack
• pushd: Push directory onto 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, the dirs command will still print the current directory. Remember: ~ stands for your home directory.
$ popd
-bash: popd: directory stack empty
$ dirs
~
$ pwd
/home/ramesh
How to use pushd and popd?
Let us first create some temporary directories.
mkdir /tmp/dir1 mkdir /tmp/dir2 mkdir /tmp/dir3 mkdir /tmp/dir4
Push these temporary directories onto the directory stack as shown below.
cd /tmp/dir1 pushd . cd /tmp/dir2 pushd . cd /tmp/dir3 pushd . cd /tmp/dir4 pushd .
After pushing the above 4 directories to the stack, let us view the content of the directory stack using the dirs command.
$ dirs
/tmp/dir4 /tmp/dir4 /tmp/dir3 /tmp/dir2 /tmp/dir1 Note: The first directory (/tmp/dir4) of the dirs command output is always the current directory, 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, bash will cd to the top directory entry in the stack and remove it from the stack. As shown above, the last directory that we pushed onto the stack is /tmp/dir4. So, when we do a popd, we will switch to /tmp/dir4 and remove it from the directory stack as shown below.
$ popd
$ pwd /tmp/dir4
After the above popd, the directory stack contains the following directories:
/tmp/dir3 /tmp/dir2 /tmp/dir1
Now, do a popd to get pop another directory from the stack and switch to it.
$ popd
$ pwd /tmp/dir3
After that popd, the directory stack contains the following directories:
/tmp/dir2
/tmp/dir1
Now, do another popd to get pop a directory from the stack and switch to it.
$ popd
$ pwd /tmp/dir2
Now, do another popd to get pop a directory from the stack and switch to it.
$ popd
$ pwd /tmp/dir1
After this popd, the directory stack is empty!
$ popd
-bash: popd: directory stack empty
Dirs
By default, dirs command prints the content of the directory stack in a single line as shown below.
$ dirs
~ /tmp/dir4 /tmp/dir3 /tmp/dir2 /tmp/dir1
Use dirs -p to print all the entries in the directory stack in a separate line:
$ dirs -p
~
/tmp/dir4
/tmp/dir3 /tmp/dir2 /tmp/dir1
Use dirs -l to show the full path of each entry:
$ dirs -l
/home/ramesh/.ssh /tmp/dir4 /tmp/dir3 /tmp/dir2 /tmp/dir1
Use dirs -c to delete all the entries on the directory stack.
$ dirs -c
Use dirs -v to print all the entries in the directory stack, each on a separate line along with its position in the stack:
$ dirs -v 0 ~
1 /tmp/dir4 2 /tmp/dir3 3 /tmp/dir2 4 /tmp/dir1
Use dirs +n to display the nth element from the directory stack. For example, to display the 3rd element, do the following:
$ dirs +3 /tmp/dir2
Use dirs -n to display the nth element (counted in reverse order) from the directory stack. For example, to display the 3rd element from the bottom, do the following:
$ dirs -3
/tmp/dir4
Note: when bash is counting from the bottom, the last element in the list is treated as the 0th element.
pushd
By default when you execute the pushd command, in addition to putting that directory onto the directory stack, bash also does a cd to that directory.
Using the pushd -n option, you can add the directory to the stack without switching to it.
Push home directory onto the stack but don’t change your working directory:
$ cd ~
$ pushd -n /etc/
$ pwd
/home/ramesh
You can also use +n or –n with pushd to rotate the directory stack upward or downward, respectively.
Let us assume the directory stack contains the following list.
$ dirs -v 0 /tmp/dir4 1 /tmp/dir4 2 /tmp/dir3 3 /tmp/dir2 4 /tmp/dir1
Using pushd +2, you can rotate the stack upward, making the 2nd element of the list (/tmp/dir3) the new top of the stack, as shown below.
$ pushd +2
/tmp/dir3 /tmp/dir2 /tmp/dir1 /tmp/dir4 /tmp/dir4
$ dirs -v 0 /tmp/dir3 1 /tmp/dir2 2 /tmp/dir1 3 /tmp/dir4 4 /tmp/dir4
Using pushd -2, you can rotate the stack downward, making the 2nd element from the end of the list the new top of the stack.
Popd
By default when you execute the popd command, bash will pick the top entry from the directory stack, cd to it, and delete that entry from the stack.
Let us assume the directory stack contains the following items, and the current directory is the home directory.
$ pwd
/home/ramesh
$ dirs -v 0 /tmp/dir4 1 /tmp/dir4 2 /tmp/dir3 3 /tmp/dir2 4 /tmp/dir1
Using popd -n, you can delete the top entry from the stack without switching to that directory.
$ popd -n
/tmp/dir4 /tmp/dir3 /tmp/dir2 /tmp/dir1
$ dirs -v 0 /tmp/dir4 1 /tmp/dir3 2 /tmp/dir2 3 /tmp/dir1
$ pwd
/home/ramesh
Using popd +2, you can delete the 2nd directory from the top of the list (/tmp/dir3 in this example). Bash will not switch to that directory.
$ dirs -v 0 /tmp/dir4 1 /tmp/dir4 2 /tmp/dir3 3 /tmp/dir2 4 /tmp/dir1
$ popd +2
/tmp/dir4 /tmp/dir4 /tmp/dir2 /tmp/dir1
$ dirs -v 0 /tmp/dir4 1 /tmp/dir4 2 /tmp/dir2 3 /tmp/dir1
Using popd -2, you can delete the 2nd directory from the bottom of the list (/tmp/dir4 in this example). Bash will not switch to that directory.
Please note that when you use -, the last item is counted as 0.
$ dirs -v 0 /tmp/dir4 1 /tmp/dir4 2 /tmp/dir2 3 /tmp/dir1
$ popd -2
/tmp/dir4 /tmp/dir2 /tmp/dir1
$ dirs -v 0 /tmp/dir4 1 /tmp/dir2 2 /tmp/dir1