Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

One I use all the time is this:

$ pwd

/Users/me/Sites/foo

$ pushd .

$ cd /

$ pwd

/

$ popd

$ pwd

/Users/me/Sites/foo

Or in English: If you're in a directory that you need to leave but you know you'll go back there in a minute, use "pushd ." to save that directory. Then go off and do whatever you need to, and when you need to return to the saved directory, use "popd" to take you straight back there.



You can also use `cd -` to return to the previous directory.


git "borrowed" this idea too. You can use `git checkout -` to checkout the last thing you checked out; very handy when merging.


Python sort of did too, you can use _ to access the last-returned variable in the interpreter.

    [testfunc(x) for x in testlist]

    <successful result>

    success = _

    print _

    <successful result>
It's useful when using the interpreter as a calculator, or when you're bashing out some calculation you can never remember how to do properly.


The same thing also works in irb.

    >> "foo"
    => "foo"
    >> bar = _
    => "foo"
    >> bar
    => "foo"


in bash you can use $_ to access the last argument of the previous command. It lets you do things like this:

    $ pwd
    /home/foo
    $ mkdir -p bar/baz/qux && cd $_
    $ pwd
    /home/foo/bar/baz/qux


Yeah but only one directory in the history. With pushd/popd you can navigate as much as you want, an then popd to that directory you intended to bookmark.


yeah, I switched to cd - for a while but got bitten by this too many times.

$ pwd

/some/long/compli/cated/v1.0.7/path/to/_stuff

$ cd /simple/

$ do -stuff

$ cd ./foo

$ do -stuff

$ rem now let's get back to that complicated directory again

$ cd -

oh, wait, I cd'ed twice, shit


If you find yourself in this spot again, Ctrl-r `cd /som` helps.


  $ pushd .
  $ cd /
Why not just

  $ pushd /
?

Also, it's not linux-specific, but bash-specific.


It's not Linux or Bash specific, pushd and popd work on Windows too, and work on network shares, e.g.

  c:\> pushd \\server\share
  z:\>


It's still provided by your shell, not the OS. Windows' cmd.exe just happens to provide pushd/popd, same as bash, zshell, etc.


     $ pushd /
oh, neat. Thanks.


You can also use $CDPATH to jump easily between directories e.g. : Imagine you have this folder ~/dev/python/my_awesome_project

If you set CDPATH to '.:~/dev/python', you can easily jump to your project just by doing cd my_awesome_project, it doesn't matter where you actually are in your FS!

I use it heavily with cd -, you should give it a try!


I really like $CDPATH, but there are tons of sloppy scripts out there that assume "cd $FOO" has no output and break when $CDPATH is set. Drives me nuts.


It's really handy for scripts too. pushd/popd definitely live up to the word awesome.

For interactive use zsh has an option called autopushd that automatically pushes directories you `cd` to. I never remember to use pushd so it's a nice convenience.


It's not just handy for scripts, I think it's imperative in scripts where you want to go in and out of a lot of directories.

  for i in foo bar baz; do
     cd $i;
     #... do something
     cd ..;
  done;
if "bar" doesn't exist, your cd .. will throw you off your original directory, whereas

  for i in foo bar baz; do
     pushd $i;
     #... do something
     popd;
  done;
you'll be guaranteed that after "popd" you're back in the right place to start a new iteration.


You really, really should use 'set -e' which will exit if there are any errors. Otherwise if 'bar' doesn't exist, you'll 'do something' in a wrong directory and frobnicate something you didn't want frobnicated.


I never was comfortable using pushd/popd.

But a few months ago found this.

https://github.com/joelthelion/autojump/wiki




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: