push/pop current directory?

68

11

With bash is there a way to push and pop the current working directory? I tried writing bash;cd dir; ./dostuff;exit; but the current directory is now dir.

user3109

Posted 2011-01-11T18:34:28.537

Reputation:

Answers

93

There is pushd and popd

Bash will keep a history of the directories you visit, you just have to ask. Bash stores the history in a stack and uses the commands pushd and popd to manage the stack.

More to read

Example:

$ pwd; pushd /tmp; pwd; popd; pwd
/home/me
/tmp ~
/tmp
~
/home/me

Nifle

Posted 2011-01-11T18:34:28.537

Reputation: 31 337

1Excerpt from the linkpushd Saves the current directory on the top of the directory stack and then cd to dir. With no arguments, pushd exchanges the top two directories. – Master Chief – 2019-01-12T08:39:43.433

32

Calling bash starts a new subshell, which has its own input; none of the other commands will run until it exits. Surrounding the commands to be run with parens will also start a new subshell, but it will run the commands within it.

( cd dir ; ./dostuff )

Ignacio Vazquez-Abrams

Posted 2011-01-11T18:34:28.537

Reputation: 100 516

1Brilliant! No need to push/pop when the intention is to just run a command in a different directory (e.g. update a dependency) then come back to the current one. – Dan Dascalescu – 2018-01-16T08:27:35.740

12

If you don't need multiple levels of directory history, you can also do:

cd foo
# do your stuff in foo
cd -

Compared to pushd/popd, this has the disadvantage that if cd foo fails, you end up in the wrong directory with cd -.

(Probably cd - is more handy outside scripts. "Let's go back where I just was.")

tuomassalo

Posted 2011-01-11T18:34:28.537

Reputation: 423

4

I use alias for keeping track of my directory changes so to 'cd' somewhere I can just go back to where I was using 'cd.', or go back two using 'cd..', etc.;

alias pushdd="pushd \$PWD > /dev/null"
alias cd='pushdd;cd'
alias ssh='ssh -A'
alias soc='source ~/.bashrc'
#below to go back to a previous directory (or more)
alias popdd='popd >/dev/null'
alias cd.='popdd'
alias cd..='popdd;popdd'
alias cd...='popdd;popdd;popdd'
alias cd....='popdd;popdd;popdd;popdd'
#below to remove directories from the stack only (do not 'cd' anywhere)
alias .cd='popd -n +0'
alias ..cd='popd -n +0;popd -n +0;popd -n +0;popd -n +0;popd -n +0;popd -n +0;popd -n +0;popd -n +0;popd -n +0;popd -n +0'

cdp

Posted 2011-01-11T18:34:28.537

Reputation: 41

1Can you explain in more detail what this code does? – bwDraco – 2015-02-17T17:31:22.397

That's clever. I have aliases around my pushd and popd to do some things I like. I use the directory stack all the time. I hate watching people cd somewhere and then scroll back looking for the previous directory to cut and paste. I can't do most of my work in my home directory because of quotas, so i have to use pooled storage on the network. – Michael Mathews – 2017-01-05T00:00:57.623

1Is the alias ..cd robust enough? it looks like it only remove the last 10 items from the stack. – reynoldsnlp – 2018-03-16T17:30:43.633