5 minutes read

You may know what a filesystem is and that it usually contains a lot of directories and files. The next step is to find out how to navigate this system. So, in this topic, we will learn how to identify your current directory, the directory you have been in before, and how to navigate to other directories.

Root directory

First, you need to understand that whatever directory you are in, you are in some part of the root directory. This is the main Linux directory that contains all other directories and files. The root is denoted as a slash /.

The addresses of all the files start at the root. For example, when you are in the home directory, its address actually is /home. All additional partitions, flash drives, or optical disks are also connected to the root directory.

Our next step is to specify the exact directory we are currently in.

pwd command

To determine which directory we are in right now there is a pwd command. Its name stands for print working directory.

You can type the pwd command in the terminal. If you run the command as soon as you open the terminal, then the result will be your home directory:

$ pwd
/home/<your username>

If you run the command from any home subdirectory open in the terminal, the result will be the full path to this subdirectory from the root directory:

$ pwd
/home/user/Documents

Now we know which directory we are in. Let's find out how to change it and navigate to other directories.

cd command

We can navigate between directories using the cd or change directory command. There are different ways to use this command.

First, if we are in the home directory, we can go directly to its subdirectory using the relative path:

$ cd Documents

In order to move to some directory within Documents, you need to perform the same action and thus go deep into the Documents. To go to the parent directory of the current one use the cd .. command.

Another way to go to another home subdirectory, for example from Documents to Music, is to specify the relative path starting with the home directory:

$ cd ~/Music/

The other way to do this is to use the relative path from the current one to the Music:

$ cd ../Music

Now we are in the Music directory. To return to the previous Documents directory, use cd -.

Whatever directory or subdirectory you end up in, you can always return to the home directory you started with. To do this, type either cd without parameters or cd ~.

The cd command is useful if you need to change your current directory to some other directory.

If you want to memorize the full sequence of path jumps there are two other commands pushd and popd. They allow you to work with the directory stack and change the current working directory in Linux and other Unix-like operating systems. Let's describe them below.

pushd command

The directory stack is a list of directories' paths you have previously navigated to. The pushd command adds the paths to directories to the stack and changes the current path to the top one.

To add a directory you should type its relative or full path. For example, let's add Downloads and Project home subdirectories paths to a stack:

$ pushd ~/Downloads
~/Downloads ~
$ pushd ~/Project
~/Project ~/Downloads ~

In case we did not add any directories' paths the stack will contain only the current working directory, and this will be the home ~ directory in our example.

We can view the stack content using the dirs -v command:

$ dirs -v
0 ~/Project
1 ~/Downloads
2 ~

Each directory path has its own index, which can be accessed using the cd command. For example, if we need to go to the Downloads directory, then we will type cd ~1.

So, this is how one can add directories paths to a stack. Below we will discuss how to pop them.

popd command

To pop a directory off the top of the stack and go to the previous directory (one after the top of the stack), one needs the popd command. Let's continue the example from the previous paragraph. The top Project directory path is popped from the stack, leaving two other paths to Downloads and ~ directories:

$ popd
~/Downloads ~

$ dirs -v
 0  ~/Downloads
 1  ~

Now the top directory in the stack is Downloads and we are in this directory. If we run the popd command one more time, we will be in the working home directory. This way we will consistently pop directories from the stack till it is empty. If you need to remove only one specific directory and it is not at the top, then you can do this by referring to it by its index:

$ popd +1
~/Downloads

$ dirs -v
0 ~/Downloads

Now you know how to pop directories off a stack.

Conclusion

To sum up,

  • With the pwd command we can determine which directory we are in,

  • The cd command is used to navigate between directories.

  • If you need to memorize the full sequence of path jumps you can use pushd and popd commands,

  • The pushd command creates a stack of directories paths,

  • The popd command pops directories off a stack.

139 learners liked this piece of theory. 4 didn't like it. What about you?
Report a typo