10 Useful cd command tips in Linux

John Gomez
6 min readSep 22, 2020

--

The ‘cd’ (change directory) command in Linux/Unix is pretty simple. It is one of the standard routine commands for every Linux user. It permits you to switch the directories from one to another via Terminal. The cd command works based on the absolute path and relative path.

If you are a newbie or Windows user, then you are probably not aware of what is a directory in Linux. It is just like a folder in Windows, and each folder in Windows can contain sub-folders. Similarly, in Linux also, the directories can have sub-directories on it. The slash (/) is a top-level directory in Linux, or we can call it as root.

Before jumping into the syntax, and the various options of the ‘cd’ command, let us memorize a few symbols which will help us to make some shortcuts.

“~”  (tild) character 	    : It always points to the home directory.“.”  (single dot) argument  : It represents the current working directory.“..” (double dot) argument  : It represents back to the parent directory.“-”  (single dash) argument : It quickly gets back to your last working directory and prints the directory path.

In Linux, the files and directories are kept in a hierarchical structure. We can navigate them by using an absolute (full) path or the relative path, that means we need to specify the exact destination path. It could be either an absolute path or a relative path.

Absolute Path : It is also called a full path where the directory always starts with the top-level (/). Here we have to specify the entire directory tree structure.

Example : ‘/var’, ‘/home/linuxteck’

Relative Path : This is the path that represents the current working directory (pwd). It never starts with a slash (/). For instance, when you are located in the home directory of linuxteck user,( i.e., /home/linuxteck) and want to access any sub-directory, then your relative path will be “linuxteck/sub-directory”.

Example : ‘linuxteck/Documents’, ‘linuxteck/Downloads’

This guide will help you to understand how to use the cd command tips in Linux with multiple options and examples to navigate the directory structure. All the examples below are tested on RHEL/CENTOS 7.6.

The Global Syntax of cd command in Linux:

$ cd /path/to/directory

1. How to change to a particular directory?

[linuxteck@centos ~]$ cd /var/www/html/

In the above example, let us assume that you are in the home directory of linuxteck (/home/linuxteck)and want to switch to the /var/www/html/ directory. The important thing is, we have to specify the correct destination directory path, which should be absolute. You can use the following command to cross-check the change in directory. In this example, it will print the absolute path of the “/var/www/html/” directory.

[linuxteck@centos html]$ pwd

Output:

/var/www/html

2. How do you change directly from the present working directory to root directory?

[linuxteck@centos html]$ cd /

In the above example, you can see a single line command can help you directly jump from your current working directory to the root, which is the top-level directory in the filesystem hierarchy. Use the following (pwd) command to see the path of the new directory location.

[linuxteck@centos /]$ pwd

Output:

/

3. How do I switch back directly to the last working directory?

[linuxteck@centos /]$ cd –

Output:

/var/www/html

As we can see in our 1st example, we have been into the /var/www/html/ directory location, and then jumped into the root directory. Now going back to the last working directory, which is /var/www/html/, use the above command. It is generally known as the shortcut command in the directory tree structure. The beauty of this command will be to print the full path of your previous working directory when the return value becomes successful.

4. How to quickly jump into the user’s home directory?

[linuxteck@centos html]$ cd OR [linuxteck@centos html]$ cd ~

You can use either one of the above commands to switch directly to the user’s home directory from any other path. The tild (~) symbol always refers to the Users home directory. For scripting, you can even use the “$HOME environment variable”.

[linuxteck@centos ~]$ pwd

Output:

/home/linuxteck

5. How to quickly navigate to a sub-directory inside your home directory?

If you are located under /var/www/html directory and want to quickly navigate to the Downloads directory, which resides inside your home directory.

[linuxteck@centos html]$ cd ~/Downloads/

Output :

[linuxteck@centos Downloads]$

The tild (~) symbol represents, it always returns immediately to the user’s home directory. In the above output, you can see, by using a single line command, we can switch immediately to a sub-directory, which is inside the home directory of LinuxTeck. Use the following (pwd) command to see the path.

[linuxteck@centos Downloads]$ pwd

Output:

/home/linuxteck/Downloads

6.How to navigate a directory by using a relative path?

The relative path always points to the present working directory location. In this example, let’s assume that I am working in the home directory of linuxteck (/home/linuxteck) and wish to change to the user’s document directory /home/linuxteck/Document. Here I use the concept of a relative path. For a better understanding, first, let’s print the current working directory.

[linuxteck@centos ~]$ pwd

Output:

/home/linuxteck

Here is the relative path.

[linuxteck@centos ~]$ cd Documents/

[linuxteck@centos Documents]$ pwd

Output:

/home/linuxteck/Documents

7. How do I change to a parent directory or one level up, a directory?

[linuxteck@centos Documents]$ cd ..

The double (..) dot argument always represents going back to the parent directory or switching one level up from the current working directory. For example, let us assume that you are currently located in the /home/linuxteck/Document directory and want to switch back to your previous working directory “/home/linuxteck”. This can be achieved by using the double (..) dot as mentioned above or provide an absolute path which is “cd /home/linuxteck”. The output of the above command will be :

[linuxteck@centos ~]$ pwd

Output:

/home/linuxteck

If you’d like to move up two directories back, then use a double-dot (../..) chain twice.

[linuxteck@centos Documents]$ cd ../..

Now you are back into one of the top-level directory /home. Use the following (pwd) command to see the path.

[linuxteck@centos home]$ pwd

Output:

/home

8. How do we change multiple levels up in the directory tree structure?

From the following directory location, you want to move up four directories back by using a single line command. In our example, you are located in a directory named green and want to switch back to the directory red. Use a double-dot (../) chain four times.

[/home/linuxteck/red/blue/white/yellow/green]$

[linuxteck@centos green]$ cd ../../../..

Again, use ‘pwd’ to verify the path. Similarly, you can chain a bunch of double (../) dots based on your needs.

9. How do you customize a cd command with an alias?

We can customize any commands in Linux/Unix to work the way we want by using the help of built-in alias commands. It will help to shorten the source commands. Let’s take the same example as #8 to change multiple levels up in a directory structure by using the alias “..n” method.

[/home/linuxteck/red/blue/white/yellow/green]$

Here let’s use the alias (..4) instead of multiple double-dots (../) chain to move me up four directories back into the directory named red. This can be achieved by adding the following entry in the current login ~/.bash_profile and then reboot your system.

alias ..4=”cd ../../../..”

After rebooting, use the “..4” alias to switch back into the directory named red from the following location.

[/home/linuxteck/red/blue/white/yellow/green]$

[linuxteck@centos green]$ ..4

[linuxteck@centos red]$

Use ‘pwd’ command to check the path :

[linuxteck@centos red]$ pwd

/home/linuxteck/red

Similarly, you can use (..3, ..2, ..1 and ..n) based on your requirements.

10. How do you switch a user’s home directory from one to another?

Based on your current login privileges, you can easily switch from your present working directory to another user’s home directory by using the tild (~) command with a username. Click here to learn more about how to set up user privileges. In this example, I will switch the user home directory from “/home/linuxteck” to “/home/john”.

[root@centos linuxteck]# cd ~john

[root@centos john]#

Use the ‘pwd’ command to check the path:

[root@centos john]# pwd

Output:

/home/john

Conclusion

Thank you! I hope this guide will help you to understand some basic usage of ‘cd’ commands. It is one of the simple commands that will tell you exactly about the directory structure where you want to go and tell whether your destination exists or not.

Drop me your feedback/comments. If you like this article, kindly share it and it may help others as well.

Thank you!

--

--

John Gomez
John Gomez

Written by John Gomez

John Gomez is a Professional Blogger and Linux consultant. You can find his work at https://www.linuxteck.com

No responses yet