How can I change to the previous directory instead of going up?
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Lost Civilization
--
Chapters
00:00 Question
01:03 Accepted answer (Score 326)
02:11 Answer 2 (Score 106)
03:16 Answer 3 (Score 13)
04:19 Answer 4 (Score 10)
04:51 Thank you
--
Full question
https://superuser.com/questions/324512/h...
Accepted answer links:
[POSIX man page for ]: http://pwet.fr/man/linux/commandes/posix...
Answer 3 links:
[autojump]: http://github.com/joelthelion/autojump
[j2]: https://github.com/rupa/j2
[z]: https://github.com/rupa/z
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#unix #shell
#avk47
ACCEPTED ANSWER
Score 326
The command
cd -
will perform the swap you need on most of the mainstream shells, the older longer variant is
cd "$OLDPWD"
which will use the environment variable that contains the previous working directory.
The POSIX man page for cd
mentions:
DESCRIPTION
If, during the execution of the above steps, the PWD environment variable is changed, the OLDPWD environment variable shall also be changed to the value of the old working directory (that is the current working directory immediately prior to the call to cd).
OPERANDS
- When a hyphen is used as the operand, this shall be equivalent to the command:
cd "$OLDPWD" && pwd
which changes to the previous working directory and then writes its name.
ANSWER 2
Score 106
In addition to bryan's answer, it's worth mentioning there's also pushd
and popd
, which build up directories like a stack. This is also available on Windows NT; however, it is not available in all shells.
For example, we can go to three different directories, and you'll always see your stack when you call pushd
:
charon:~ werner$ pushd Documents/
~/Documents ~
charon:Documents werner$ pushd ../Movies/
~/Movies ~/Documents ~
charon:Movies werner$ pushd ../Downloads/
~/Downloads ~/Movies ~/Documents ~
And when you call popd
three times in a row, you get to those directories in the stack in reverse order. At the same time, the stack will be emptied again.
charon:Downloads werner$ popd
~/Movies ~/Documents ~
charon:Movies werner$ popd
~/Documents ~
charon:Documents werner$ popd
~
charon:~ werner$ popd
-bash: popd: directory stack empty
If you are using Zsh; it has an AUTO_PUSHD option, which will automatically push cd
's onto the stack.
ANSWER 3
Score 13
There are some "jump" programs
- autojump (maintained with basic features)
- j2 (apparently unmaintained with some advanced features)
- z (maintained version of "j" with advanced features)
These ease any kind of directory navigation. You use it by giving a portion of the path and it just works.
In your case
~$ j baz
/etc/foo/bar/baz/moo$
~$ j bla
/var/lib/fubarred_app/blargh/logs$
You can assign any letter you want to these programs, "j" is tradition :)
j2 and z support multiple search terms,...
~$ j baz src
/home/me/projects/baz/repository/trunk/src$
... and more options.
~$ j -l # list directories by "frecency"(frequency + recency) score
~$ j -r PATTERN # match by rank only, not recency
~$ j -t PATTERN # match by recency only, not rank
ANSWER 4
Score 10
You can use this to easily make aliases for directories:
a() { alias $1=cd\ $PWD; }
a 1
and later:
1