Posted: . At: 9:19 AM. This was 4 years ago. Post ID: 14243
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.


Switch back and forth between directories in Linux with this simple cd trick.


I have found a very nice trick to switch back and forth between directories in Linux. This is very useful if the user needs to do something in one directory and then switch back to the previous directory to do something else. Below is a very useful example.

I cd from ~ to the Documents directory. Then type cd – and I am moved back to the ~ directory.

4.4 Wed Apr 15 jason@Yog-Sothoth 1: $ cd Documents/
4.4 Wed Apr 15 jason@Yog-Sothoth 1: $ cd -
/home/jason

This is very useful for rapidly switching back and forth between two directories.

4.4 Wed Apr 15 jason@Yog-Sothoth 1: $ cd -
/home/jason/Documents
4.4 Wed Apr 15 jason@Yog-Sothoth 1: $ cd -
/home/jason

This could be a very good trick for any UNIX or Linux user.

The CDPATH environment variable is very useful as another Linux trick. This sets the path that the cd command uses to look for the directory that you are intending to move to.

Here is an example.

jason@Yog-Sothoth:~$ export CDPATH=/home/jason/Documents/

I am in the ~ directory, but when I run this command, I am moved to the /home/jason/Documents/Megaphon directory.

jason@Yog-Sothoth:~$ cd Megaphon/
/home/jason/Documents/Megaphon
jason@Yog-Sothoth:~/Documents/Megaphon$

This is also a very useful Linux trick.


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.