Using pushd and popd in Linux

tux-logoMost 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

Advertisements

About synapse
Programming, motorcycles and photography. Want to do more, but only have time for so much!

13 Responses to Using pushd and popd in Linux

  1. Ruzan says:

    Dude,

    This is gonna help me so much 🙂 I traverse folders sooooo many times during my work in cygwin…. have created aliases 😀 Thanks!

  2. synapse says:

    You’re welcome dude 😉

  3. roan says:

    beautiful online information center. greatest work… thanks

  4. Synapse says:

    glad to see its helping people

  5. Pingback: 2000 apollo

  6. Pingback: green white red

  7. hgjhgg says:

    9awad nta w lexplication dialek

  8. Pingback: shiftMode » Blog Archive » Pushin and Poppin

  9. Pingback: Petryx » Usando pushd e popd no linux

  10. Raymond says:

    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

  11. Pingback: Study Guide: 2009 Linux+ Objectives - TechExams.net IT Certification Forums

  12. Pingback: Using pushd and popd in Linux « Mike's Blog

  13. suraj says:

    came across this while trying LFS, everybody explains how to use it but only u said why to use. thanks. By the way nice about me, i’m thinking about copying it minus the developer part

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: