I have git repos stored in different directories which have long paths /opt/too/long/path/to/type/every/time/git-repo
and it's kind of difficult to remember plus to cd
to the directory every time is a pain and tedious. (I know TAB helps but that gets tedious too.) So, my life would be much easier if I could store the pwd
somewhere and later do something like cd thatgitrepo
.

- 228
8 Answers
Make yourself some symbolic links in your home directory:
ln -s /opt/really/long/obnoxiously/long/path/to/some/projectname
This command will make a link called projectname
in your home directory into which you can cd
, and everything will work perfectly.

- 76,081
z
is a very nice "plugin" for bash
or zsh
(it's included in Oh My Zsh) which keeps track of directories you cd
to, and allows you to quickly switch to directories using parts of their names, based on how frequently you use them.
So after a little while, it will know that
z git-repo
should cd
to the git-repo
directory you use most often. You can qualify that with part of the path, e.g.
z d git-repo
or even
z d git
I've found this to be more useful than aliases or even CDPATH
because it learns on its own and adapts to my changing habits.

- 434,908
Once inside the directory, you do:
repo=`pwd`
and later you do
cd $repo
If you want to keep it for next time, you could do:
echo "export repo=`pwd`" >> ~/.profile

- 7,622
bash
's CDPATH
shell variable might be a convenient solution for you. A command such as cd foo
searches for the subdirectory called foo
inside the directories listed in CDPATH
.

- 5,866
Just to mention another useful tool, there is the pushd
builtin. With
$ pushd dirname
the current directory will be pushed on the directory stack (you can look at that stack with dirs
) and the current directory will be changed to dirname
. You can later change back to the latest (top-most in the stack) directory using the popd
builtin command.
This should work at least in bash, zsh, csh and tcsh.
If you just want to change to the previous directory you have been in, cd -
comes to help.

- 3,723
Quoting this answer by ramesh:
The command you are looking for is
pushd
andpopd
.You could view a practical working example of
pushd
andpopd
from here.mkdir /tmp/dir1 mkdir /tmp/dir2 mkdir /tmp/dir3 mkdir /tmp/dir4 cd /tmp/dir1 pushd . cd /tmp/dir2 pushd . cd /tmp/dir3 pushd . cd /tmp/dir4 pushd . dirs /tmp/dir4 /tmp/dir4 /tmp/dir3 /tmp/dir2 /tmp/dir1
-
1
-
If using zsh,
setopt autopushd
makes everycd
apushd
, which is fantastic in that you no longer have to remember to use a different command. – Xiong Chiamiov May 13 '16 at 20:09
For coding stuff I have always just used environment variables to store commonly used code directories.
In a bash file that gets executed do:
export PROJECTNAMEDIR=/opt/too/long/path/to/type/every/time/git-repo
Then you can do stuff like cd $PROJECTNAMEDIR
or git checkout $PROJECTNAMEDIR
and also variables will autocomplete with bash.

- 101
-
A great answer! Except... largely redundant with Julie Pelletier's already-existing answer. – TOOGAM May 14 '16 at 09:32
-
cd
into some well know directories. – dan May 13 '16 at 10:32CDPATH
. It has many advantages over symlinks. – alexis May 13 '16 at 11:43