pushd & popd

Unlock the Magic of Linux Folders with pushd and popd!

ยท

2 min read

pushd & popd

Ever wished navigating folders in Linux was like playing with a magical file cabinet? Meet pushd and popd โ€“ your command line buddies that make switching between folders as easy as opening and closing drawers. It's like a cool trick for your computer! Ready to peek inside the magic? Let's dive in! ๐Ÿš€๐Ÿ—‚๏ธ

pushd

  • It pushes a directory onto the directory stack and changes to that directory.

  • Imagine you have a stack of plates. You add a new plate on top (representing a directory), and now you are working with the top plate.

      $ pushd /path/to/directory
    

popd

  • Pops the top directory off the directory stack and changes to that directory.

  • Imagine you take the top plate off the stack (representing a directory) and return to the one beneath it.

      popd
    

Directory Stack

  • A stack is like a pile of plates (directories) where the top plate is the one you are currently working in.

  • It's like a cafeteria tray stack. The tray at the top is the one you are using, and when you popd, you take the top tray off.

Example: File Cabinet

  • Jack is currently working in the Rio directory.

  • Jack needs to work on something in the "Berlin" directory. So, he pushed the current directory in and opened the "Berlin" directory, using the given command.
pushd Berlin
  • Now, Jack is in the "Berlin" directory, making changes.

  • After completing the work in "Berlin," Jack wants to go back to the "Rio" directory. He pops the "Berlin" drawer off and returns to "Rio", using:
popd

This analogy helps visualize how pushd pushes the current directory onto the stack, and popd pops the top directory off the stack, allowing you to switch between directories easily, just like opening and closing drawers in a file cabinet.

ย