News for hackers. Stuff that matters.
Re:Awesome but commonly unknown Linux Commands by user24 on Wednesday August 10, 2011 @05:42AM (#2867457)
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.

Re:Awesome but commonly unknown Linux Commands by nfm on Wednesday August 10, 2011 @05:54AM (#2867478)
You can also use `cd -` to return to the previous directory.
Re:Awesome but commonly unknown Linux Commands by footle on Wednesday August 10, 2011 @05:57AM (#2867481)
git "borrowed" this idea too. You can use `git checkout -` to checkout the last thing you checked out; very handy when merging.
Re:Awesome but commonly unknown Linux Commands by nosignal on Wednesday August 10, 2011 @07:45AM (#2867761)
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.
Re:Awesome but commonly unknown Linux Commands by brettnak on Wednesday August 10, 2011 @01:31PM (#2869254)
The same thing also works in irb.

    >> "foo"
    => "foo"
    >> bar = _
    => "foo"
    >> bar
    => "foo"
Re:Awesome but commonly unknown Linux Commands by LiveTheDream on Thursday August 11, 2011 @12:39AM (#2871418)
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
Re:Awesome but commonly unknown Linux Commands by leviathan on Wednesday August 10, 2011 @07:38AM (#2867741)
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.
Re:Awesome but commonly unknown Linux Commands by user24 on Wednesday August 10, 2011 @08:26AM (#2867861)
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

Re:Awesome but commonly unknown Linux Commands by nickknw on Wednesday August 10, 2011 @01:41PM (#2869304)
If you find yourself in this spot again, Ctrl-r `cd /som` helps.
Re:Awesome but commonly unknown Linux Commands by cema on Wednesday August 10, 2011 @06:03AM (#2867501)

  $ pushd .
  $ cd /
Why not just

  $ pushd /
?

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

Re:Awesome but commonly unknown Linux Commands by jodrellblank on Wednesday August 10, 2011 @06:57AM (#2867633)
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:\>
Re:Awesome but commonly unknown Linux Commands by nuclear_eclipse on Wednesday August 10, 2011 @10:17AM (#2868278)
It's still provided by your shell, not the OS. Windows' cmd.exe just happens to provide pushd/popd, same as bash, zshell, etc.
Re:Awesome but commonly unknown Linux Commands by user24 on Wednesday August 10, 2011 @07:11AM (#2867666)

     $ pushd /
oh, neat. Thanks.
Re:Awesome but commonly unknown Linux Commands by alexis-d on Wednesday August 10, 2011 @10:15AM (#2868270)
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!

Re:Awesome but commonly unknown Linux Commands by technomancy on Wednesday August 10, 2011 @12:33PM (#2868994)
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.
Re:Awesome but commonly unknown Linux Commands by sjs on Wednesday August 10, 2011 @05:57AM (#2867482)
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.

Re:Awesome but commonly unknown Linux Commands by leviathan on Wednesday August 10, 2011 @07:47AM (#2867763)
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.
Re:Awesome but commonly unknown Linux Commands by phaker on Wednesday August 10, 2011 @08:57AM (#2867944)
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.
Re:Awesome but commonly unknown Linux Commands by huherto on Wednesday August 10, 2011 @10:49AM (#2868460)
I never was comfortable using pushd/popd.

But a few months ago found this.

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