One of my favorite ways to save time on the command-line is to utilize the directory stack to jump between tasks. Today’s article will show you how to do this and provide some tips for effective use.
What is the directory stack?
Most Linux environments have a way for you to put paths on a stack (push) and then take them off in reverse order (pop). This is useful when you have more than one directory that you need to switch between frequently. Let’s take a look at how to do this.
The pushd command
Suppose you need to switch between your project: ~/src/myproject, your web-server: /opt/webserver7/logs and some code examples: ~/examples/othercode often.
You add things to the directory stack with pushd like so:
Now your in your server logs and you want to go to the examples:
The popd command
Now we want to go back to my project to deploy it to our web server. Since we have our directory stack working we can do this quickly:
For the purposes of this example, let’s pretend we don’t care to go back to the examples. Let’s remove it from the stack:
One final pop will get me back to my server logs once I test it out in my browser. Of course, we can keep using the stack continuously.
Alternatives and nifty tips
You might not always want to use the directory stack. You can use these really fast shortcuts to navigate to home or back quickly:
Now for some final examples that may help out just a little more:
Using pushd and popd effectively can help you get around your command-line environment quickly. See the MAN pages for more information.
Take a bit of time to get the hang of it now and it’ll pay off later. Share your other examples or questions!