In terminal, how can I define a key to go to the previous directory which I was in when changing directory with the cd
command?
For example, I'm in /opt/soft/bin
and I cd
into /etc/squid3
and I want to get back to the first directory.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityIn terminal, how can I define a key to go to the previous directory which I was in when changing directory with the cd
command?
For example, I'm in /opt/soft/bin
and I cd
into /etc/squid3
and I want to get back to the first directory.
You can use
cd -
or you could use
cd "$OLDPWD"
cd -
. For example when you type cd /usr/local
you know that you are in /usr/loal
, but when you type cd -
you don't always remember from which directory you came from. So it saves you from typing cd -; pwd
. But this is all speculation.
– Raphael Ahrens
Sep 1 '15 at 4:37
alias -- -='cd -'
then use -
(1 char) instead of cd -
(4 char). Faster :D
– ADTC
Feb 16 '16 at 4:38
cd -
twice. Do cd /; cd /usr; cd -; cd -
you should be in /usr
. But maybe I miss understood your question.
– Raphael Ahrens
Jan 7 '19 at 19:42
The other answers are definitely complete in the direct answer sense. cd -
and cd $OLDPWD
are definitely the main choices for this. However, I often find that getting into a workflow with pushd
and popd
works better.
Long story short, if you are moving into a directory with the ultimate intent of coming back to where you started, use pushd
/popd
.
The major difference is easily shown by an example.
$ cd dir1
$ pushd dir2
At this point, you have a directory stack that is dir2, dir1
. Running pushd
with no arguments will put you back in dir1
with the stack now as dir1, dir2
. popd
would do the same, but would leave you with an empty directory stack. This is not much different than how you would have been with the cd -
workflow.
However, now you can now change directories multiple times and get back to dir1
. For example,
$ cd dir1
$ pushd dir2
$ cd dir3
If you run popd
at this point, you will go back to dir1
.
pushd
and go back to previous folder while popd
-ing.
– Bernhard
Jun 30 '13 at 19:08
dirs -v
, but the reality for me is that often the simple case is all I really use. (Or, worse, I make a mistake when trying push +2
or similar) Also, I thought baby steps for getting someone to try the workflow. :)
– Josh Berry
Jul 1 '13 at 2:53
popd
at all? Could you not just cd
and pushd
everywhere? Are there any disadvantages to doing so?
– Hashim Aziz
Nov 12 '19 at 16:35
You should use:
cd ~-
it does the same as cd -
(from the currently accepted answer) without the annoying echo of the directory and is easier to type than cd "$OLDPWD"
or cd - > /dev/null
.
alias -- -='cd "$OLDPWD"'
then using -
(1 char) instead of cd ~-
(5 char). :)
– ADTC
Feb 16 '16 at 4:37
cd ~-/..
works but cd -/..
does not!
– Ray Foss
Jun 7 '19 at 14:27
You can "define a key" for cd -
by editing your ~/.bashrc
file and including an alias for the command. For example you could add cdc
to make it cd -
which would provide you with a shorter way to get to the last directory by adding:
alias cdc='cd -'
This way you would simply type cdc
and it would put you in your last working directory.
bind '"\e[24~":"\C-k \C-ucd -\n"'
(or more preferably move it to inputrc
file as mentioned in the answer).
– ADTC
Feb 16 '16 at 5:00
cd ..
goes to the precedent folder in the folder's tree.
cd -
goes to the folder which it was before. This command didn't work on some distros (ubuntu 16.04), works in debian 9.