4 ms·
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 dire
by user24 15y ago
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.
- nfm 15y agoYou can also use `cd -` to return to the previous directory.
- footle 15y agogit "borrowed" this idea too. You can use `git checkout -` to checkout the last thing you checked out; very handy when merging.
- nosignal 15y agoPython 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.
- brettnak 15y agoThe same thing also works in irb. >> "foo" => "foo" >> bar = _ => "foo" >> bar => "foo"
- LiveTheDream 15y agoin 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
- leviathan 15y agoYeah 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.
- user24 15y agoyeah, 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
- nickknw 15y agoIf you find yourself in this spot again, Ctrl-r `cd /som` helps.
- sjs 15y agoIt'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.
- leviathan 15y agoIt'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.
- phaker 15y agoYou 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.
- cema 15y ago$ pushd . $ cd / Why not just $ pushd / ? Also, it's not linux-specific, but bash-specific.
- jodrellblank 15y agoIt'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:\>
- nuclear_eclipse 15y agoIt's still provided by your shell, not the OS. Windows' cmd.exe just happens to provide pushd/popd, same as bash, zshell, etc.
- user24 15y ago$ pushd / oh, neat. Thanks.
- alexis-d 15y agoYou 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!
- technomancy 15y agoI 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.
- huherto 15y agoI never was comfortable using pushd/popd. But a few months ago found this. https://github.com/joelthelion/autojump/wiki https://github.com/joelthelion/autojump/wiki