I remember there was a way to execute the cd
command, automatically returning to the previous directory (without an explicit cd ...
).
Is it possible?
Asked
Active
Viewed 4,367 times
9

Peter Mortensen
- 30,738
- 21
- 105
- 131

Roberto Aloi
- 30,570
- 21
- 75
- 112
-
Just to be curious, why that vote as "not a real question"? :S Please leave a comment and I'll try to clarify... – Roberto Aloi Mar 09 '10 at 11:21
4 Answers
19
If you just want to go back to the last directory, you can use cd -
.
If you need more places to go back to, try pushd <dir>
(instead of cd <dir>
) and then you can go back with popd
.

Aaron Digulla
- 321,842
- 108
- 597
- 820
-
Accepting. Even if I'm starting a couple of sub-shells in my own script, this sounds as the best alternative. – Roberto Aloi Mar 09 '10 at 11:28
4
Found! I can execute it as a sub-shell.
A command list embedded between parentheses runs as a subshell.

Roberto Aloi
- 30,570
- 21
- 75
- 112
-
2Do note that running it in a subshell means that things like setting environment variables to be used outside won't work. – Ignacio Vazquez-Abrams Mar 09 '10 at 11:16
-
2Strictly speaking you don't "return" to the current directory since you never left it in the scope of the parent shell. – sorpigal Mar 09 '10 at 11:17
-
@Ignacio: Yep, but for what I need is fine. @Sorpigal: Good point. I'm aware of that :) – Roberto Aloi Mar 09 '10 at 11:20
1
Also, pushd and popd can come in very handy - they represent operations on a stack of directory locations - allowing you to "travel back in time".

vstrien
- 2,547
- 3
- 27
- 47
1
You can also try the cdargs
package. There is a tutorial.
Summary:
yum -y install compat-libstdc++-296
wget http://www.skamphausen.de/downloads/cdargs/cdargs-1.31-1.i386.rpm
rpm -ivh cdargs-1.31-1.i386.rpm
updatedb
source `locate cdargs-bash.sh`
# You can use any method you like for locating this file and running it
# Also, you need to put it in the ~/.bash_profile file to run it every time you log in
echo source `locate cdargs-bash.sh` >> ~/.bash_profile
cdb
Mark a directory and move around:
cd /etc/conf/httpd/ext/
mark apaconf
cd /usr/local/src/
cdb apaconf
See the link for other commands or search the Internet.

Peter Mortensen
- 30,738
- 21
- 105
- 131

recluze
- 1,920
- 4
- 21
- 34