Using pushd and popd in Linux
Most people don't use the pushd and popd commands. But these commands can be really useful if you work a lot at the command line. There are many times when you wish to go into a directory that is deeply nested, only to realise that you have to get back to the directory that you were previously working in.
This is where the pushd and the popd commands come in handy. You can push the current directory onto a stack using the pushd command. Then you can switch to any directory you wish. If you wish to get back to the directory that you had earlier pushed onto the stack all you have to do is use the popd command.
We are going to make our lives even easier by creating aliases (kinda like a shorcut) for the pushd and popd commands. If you use bash (which is very common), follow these steps (everything in bold is to be typed at the terminal) -
cd /home/admin
Replace admin with your user name. The purpose of this command is to switch to the home directory.
gedit .bashrc
This will open the .bashrc file in a text editor. Type the following lines at the end of the file -
alias +='pushd .'
alias _='popd'
Now click on the save button and exit gedit. You are done! Hence forth to push a directory on the stack simply type + on the command line. If you wish to get back to the directory you pushed onto the stack simply type _ on the command line followed by a carriage return (enter)
Note - If you push more than one directory on the stack then it gets popped in a LIFO manner i.e Last In First Out


















Dude,
This is gonna help me so much
I traverse folders sooooo many times during my work in cygwin…. have created aliases
Thanks!
You’re welcome dude
beautiful online information center. greatest work… thanks
glad to see its helping people
2000 apollo
SomegifttoME 136616 homepage of 2000 apollo
Trackback by 2000 apollo | February 3, 2007 |
green white red
SomegifttoME 136616 green white red info
Trackback by green white red | February 3, 2007 |
9awad nta w lexplication dialek
[...] If you end up using these commands then I highly recommend this tip. [...]
Pingback by shiftMode » Blog Archive » Pushin and Poppin | July 21, 2008 |
[...] no blog Synapse, um post onde o autor criou um alias para o pushd e o pop [...]
Pingback by Petryx » Usando pushd e popd no linux | August 23, 2008 |
Thank you for the info – good to know.
However, not too useful in current example. I work on the command line all the time. I avoid using relative paths, period. I always go “cd /blah/blah/blah” and strongly advise against “cd blah/blah”. While you might need to type a lot bash completion makes it easy on you and it’s not that bad. The good thing about the method I use is that you will never by mistake type “rm -rf usr” and wipe out your whole /usr when you found out you were in “/” while you thought you were at /tmp/package-1.2.3
If you hate writing directories you can also do something like:
export $Z=/usr/bin/min/cin/din/pin/etc/etc/etc
and then do whatever you want:
ls -al $Z
touch $Z/file.123
[...] and Operations * Directory navigation. cd ls Linux Command Directory: ls – O'Reilly Media pushd Using pushd and popd in Linux Synapse ? popd Using pushd and popd in Linux Synapse ? pwd * File commands file Linux [...]
Pingback by Study Guide: 2009 Linux+ Objectives - TechExams.net IT Certification Forums | August 21, 2009 |
[...] via Using pushd and popd in Linux « Synapse ↵. [...]
Pingback by Using pushd and popd in Linux « Mike's Blog | October 9, 2009 |