How to use the pushd command for more efficient directory navigation on Linux

Learn how to populate a directory stack so you can more efficiently navigate around the Linux directories you use most.

How to use the pushd command for more efficient directory navigation on Linux

Most Linux admins eventually wind up using the terminal window. Within that tool, you'll navigate through the directory hierarchy using the cd command. But on those days when you're constantly moving in and out of directories, having to type out those long paths can be a real efficiency killer. 

What if there was a way to make this easier? 

There is, thanks to the pushd command. 

SEE: 10 things companies are keeping in their own data centers (TechRepublic download)

With pushd you can populate a directory stack and easily move between them. Say you'll be working within /etc/ /var/log/ /var/www/html /etc/apache2/sites-available and ~/ throughout the day. Why bother typing out those directories when you can make things easier? 

Let me show you how. 

Populate the pushd stack with the directories you want with the following commands (modify as needed):

pushd /etc
pushd /var/log/
pushd /var/www/html
pushd /etc/apache2/sites-available
pushd ~/

At this point your stack is populated and each entry will have an associated number. 

To find those numbers, issue the command:

dirs -v

With those numbers in hand, you can quickly switch to any of those directories with the command pushd +X, where X is the number associated with the directory you want to change into. 

You can add as many directories as you like to the stack, and bouncing around from those directories is far easier now. Enjoy your new-found efficiency.

Also see

linuxadminhero.jpg

Image: Jack Wallen