Navigating between Directories (or Files?)
In this document, we will focus on commands and techniques that may speed up our Linux navigation between filesystems.
This document assumes that you have intermediate Linux knowledge, if you are a beginner, please refer this link.
$HOME of user
Normally the user's home will be located in /home
directory, some of them are not, but you always can quickly move there using this way ~ + <name>
$ pwd
/tmp/castle
$ cd ~stackops
$ pwd
/home/stackops
CDPATH
Sometimes, there are directories that we use more than others. Let's say you have 3 projects: A, B and C. The source code for each of them:
A -> /tmp/org1/team1/projectA/
B -> /tmp/org1/team2/projectB/
C -> /tmp/org2/team2/projectC/
$ tree org*
org1
├── team1
│ └── projectA
└── team2
└── projectB
org2
├── team1
└── team2
└── projectC
To go to projectA, we must cd /tmp/org1/team1/projectA/
, it's the same for projectB and projectC and they are tedious. To quickly overcome this, we can use something called CDPATH
.
Like PATH
stores the location of the commands, `CDPATH`` stores the directories location
$ export CDPATH=/tmp/org1/team1:/tmp/org1/team2:/tmp/org2/team2
# append those directory into CDPATH
# we can go to project[A,B,C] from any directory
$ pwd
/tmp
$ cd project<tab>
projectA/ projectB/ projectC/
$ cd projectA
/tmp/org1/team1/projectA
$ pwd
/tmp/org1/team1/projectA
$ cd projectC
/tmp/org2/team2/projectC
Switch between directories
Entering a long path in Linux is boring, hence they provide some techniques for quickly navigating between the current and previous directories
-
symbol
This character has the same meaning as the environment variable OLDPWD
in Linux. We can use it to return the previous directory:
$ pwd
/tmp/org2/team2/projectC
$ cd /tmp/
$ pwd
/tmp
$ cd -
/tmp/org2/team2/projectC
$ pwd
/tmp/org2/team2/projectC
$ cd -
/tmp
$ pwd
/tmp
With -
, so far we can move between only 2 directories. If we want more flexible options, let's use the following technique
toggle stack: pushd / popd / dirs
The overall idea is that we can push our frequently used directories into the stack
and use it later.
The concept of stack
is first in last out, what is put into first will be taken last.
Let's go back to our previous 3 project examples.
A -> /tmp/org1/team1/projectA/
B -> /tmp/org1/team2/projectB/
C -> /tmp/org2/team2/projectC/
Before we move on, let's get the basic concept of each command.
pushd: push a directory into the stack
$ pwd
/tmp
$ pushd /tmp/org1/team1/projectA/
# push projectA path into the stack
/tmp/org1/team1/projectA /tmp
# do the same for projectB and projectC
$ pushd /tmp/org1/team2/projectB/
/tmp/org1/team2/projectB /tmp/org1/team1/projectA /tmp
$ pushd /tmp/org2/team2/projectC/
/tmp/org2/team2/projectC /tmp/org1/team2/projectB /tmp/org1/team1/projectA /tmp
popd: get the directory out of the stack
$ pwd
/tmp/org2/team2/projectC
$ popd
/tmp/org1/team2/projectB /tmp/org1/team1/projectA /tmp
$ popd
/tmp/org1/team1/projectA /tmp
# everytime we `popd`
# we can see the `stack` is reduced one by one
$ popd
/tmp
dirs: list the current stack
Let's re-pushd
the contents and see.
$ dirs
/tmp/org2/team2/projectC /tmp/org1/team2/projectB /tmp/org1/team1/projectA /tmp
$ dirs -v
0 /tmp/org2/team2/projectC
1 /tmp/org1/team2/projectB
2 /tmp/org1/team1/projectA
3 /tmp
# index 0 is the top stack
Action
## to switch between top two directories of the stack (index 0 and 1)
$ pwd
/tmp/org2/team2/projectC
$ pushd
/tmp/org1/team2/projectB /tmp/org2/team2/projectC /tmp/org1/team1/projectA /tmp
$ pwd
/tmp/org1/team2/projectB
$ pushd
/tmp/org2/team2/projectC /tmp/org1/team2/projectB /tmp/org1/team1/projectA /tmp
$ pwd
/tmp/org2/team2/projectC
## to switch to other directories in the stack
$ dirs -v
0 /tmp/org2/team2/projectC
1 /tmp/org1/team2/projectB
2 /tmp/org1/team1/projectA
3 /tmp
$ pushd +2
# +2 is also the index, mean projectA
/tmp/org1/team1/projectA /tmp /tmp/org2/team2/projectC /tmp/org1/team2/projectB
$ pwd
/tmp/org1/team1/projectA
$ dirs -v
0 /tmp/org1/team1/projectA
1 /tmp/org1/team2/projectB
2 /tmp
3 /tmp/org2/team2/projectC
$ pushd -0
# in this case
# -0 mean +3, mean project C
# -1 mean +2
/tmp/org2/team2/projectC /tmp/org1/team1/projectA /tmp/org1/team2/projectB /tmp
$ pwd
/tmp/org2/team2/projectC
We can shortcut these commands by alias
alias ad=pushd
alias pd=popd
alias drs="dirs -v"