Linux Shell Scripting Cookbook(Third Edition)
上QQ阅读APP看书,第一时间看更新

How to do it...

The pushd and popd commands replace cd for changing your working directory.

  1. To push and change a directory to a path, use this command:
        ~ $ pushd /var/www

Now the stack contains /var/www ~ and the current directory is changed to /var/www.

  1. Now, push the next directory path:
        /var/www $ pushd /usr/src

 Now the stack contains /usr/src/var/www ~ and the current directory is /usr/src.

You can push as many directory paths as needed.

  1. View the stack contents:
        $ dirs
        /usr/src /var/www ~ /usr/share /etc
        0           1              2  3              4      
  1. Now when you want to switch to any path in the list, number each path from 0 to n, then use the path number for which we need to switch. Consider this example:
        $ pushd +3

 Now it will rotate the stack and switch to the /usr/share directory.

 pushd will always add paths to the stack. To remove paths from the stack, use popd.

  1. Remove a last pushed path and change to the next directory:
        $ popd

Suppose the stack is /usr/src /var/www ~ /usr/share /etc, and the current directory is /usr/src. The popd command will change the stack to /var/www ~ /usr/share /etc and change the current directory to /var/www.

  1. To remove a specific path from the list, use popd +numnum is counted as 0 to n from left to right.