Chapter 8
Moving Around (pushd, popd)

8.1 Do This


Source 13: Linux/Mac OSX Exercise 8
  $ cd temp
  $ mkdir -p i/like/icecream
  $ pushd i/like/icecream
  ~/temp/i/like/icecream ~/temp
  $ popd
  ~/temp
  $ pwd
  ~/temp
  $ pushd i/like
  ~/temp/i/like ~/temp
  $ pwd
  ~/temp/i/like
  $ pushd icecream
  ~/temp/i/like/icecream ~/temp/i/like ~/temp
  $ pwd
  ~/temp/i/like/icecream
  $ popd
  ~/temp/i/like ~/temp
  $ pwd
  ~/temp/i/like
  $ popd
  ~/temp
  $ pushd i/like/icecream
  ~/temp/i/like/icecream ~/temp
  $ pushd
  ~/temp ~/temp/i/like/icecream
  $ pwd
  ~/temp
  $ pushd
  ~/temp/i/like/icecream ~/temp
  $ pwd
  ~/temp/i/like/icecream
  $


Source 14: Windows Exercise 8
  > cd temp
  > mkdir -p i/like/icecream
  
  
      Directory: C:\Users\zed\temp\i\like
  
  
  Mode                LastWriteTime     Length Name
  ----                -------------     ------ ----
  d----        12/20/2011  11:05 AM            icecream
  
  
  > pushd i/like/icecream
  > popd
  > pwd
  
  Path
  ----
  C:\Users\zed\temp
  
  
  > pushd i/like
  > pwd
  
  Path
  ----
  C:\Users\zed\temp\i\like
  
  
  > pushd icecream
  > pwd
  
  Path
  ----
  C:\Users\zed\temp\i\like\icecream
  
  
  > popd
  > pwd
  
  Path
  ----
  C:\Users\zed\temp\i\like
  
  
  > popd
  >

8.2 You Learned This

You're getting into programmer territory with these commands, but they're so handy I have to teach them to you. These commands let you temporarily go to a different directory and then come back, easily switching between the two.

The pushd command takes your current directory and "pushes" it into a list for later, then it changes to another directory. It's like saying, "Save where I am, then go here."

The popd command takes the last directory you pushed and "pops" it off, taking you back there.

Finally, on Unix pushd, if you run it by itself with no arguments, will switch between your current directory and the last one you pushed. It's an easy way to switch between two directories. This does not work in PowerShell.

8.3 Do More

  1. Use these commands to move around directories all over your computer.
  2. Remove the i/like/icecream directories and make your own, then move around in them.
  3. Explain to yourself the output that pushd and popd print out to you. Notice how it works like a stack?
  4. You already know this, but remember that mkdir -p will make an entire path even if all the directories don't exist. That's what I did very first for this exercise.


Online Video Course + PDF For $9

For the price of most other course's PDFs only, you can get the full PDF for this class and 2 videos demonstrating the whole book for both Unix/OSX Terminal and Windows PowerShell. The course is self-paced so you can go through it any time you want, as many times as you want.


Signup Now At Udemy.com For $9