walking through directories with pushd and popd

6 August, 2005 - 13:28
Categories:

When you're spending a part of your life on the commandline, you'r probably cd'ing a lot in and out directories. Some handy commands (for bash):

  • cd (without arguments) brings you to your home directory
  • cd - (with just a minus) brings you to the previous directory you went to with cd
  • pushd works like cd but stores it argument on a stack. popd pops the last directory from that stack and goes back to the previous directory on the stack. With dirs you can view your current directory stack (which do popd and pushd too). That way you can go back in your directory visitation round.
    As an example:
    /home/foobear $> pushd work/bee/wellohorld
    ~/work/bee/wellohorld ~
    /home/foobear/work/bee/welloworld $> pushd ~/tmp/
    ~/work/bee/tmp ~/work/bee/welloworld ~
    /home/foobear/tmp $> pushd /etc/
    /etc ~/work/bee/tmp ~/work/bee/welloworld ~
    /etc $> popd
    ~/work/bee/tmp ~/work/bee/welloworld ~
    /home/foobear/tmp $> pushd
    ~/work/bee/wellohorld ~
    /home/foobear $>