10 Useful cd command tips in Linux

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.

Note:

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?

[[email protected] ~]$ cd /var/www/html/

Note:

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.

[[email protected] html]$ pwd

Output:

 

/var/www/html

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

[[email protected] html]$ cd /

Note:

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.

[[email protected] /]$ pwd

Output:

 

/

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

[[email protected] /]$ cd –

Output:

 

/var/www/html

Note:

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?

[[email protected] html]$ cd                          OR                          [[email protected] html]$ cd ~

Note:

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”.

[[email protected] ~]$ pwd

Output:

 

/home/linuxteck

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

Note:

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

[[email protected] html]$ cd ~/Downloads/

Output :

 

[[email protected] Downloads]$

Note:

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.

[[email protected] Downloads]$ pwd

Output:

 

/home/linuxteck/Downloads

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

Note:

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.

[[email protected] ~]$ pwd

Output:

 

/home/linuxteck

Here is the relative path.

[[email protected] ~]$ cd Documents/

[[email protected] Documents]$ pwd

Output:

 

/home/linuxteck/Documents

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

[[email protected] Documents]$ cd ..

Note:

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 :

[[email protected] ~]$ pwd

Output:

 

/home/linuxteck

Note:

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

[[email protected] Documents]$ cd ../..

Note:

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

[[email protected] home]$ pwd

Output:

 

/home

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

Note:

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]$

[[email protected] green]$ cd ../../../..

Note:

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?

Note:

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]$

Note:

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 ../../../..”

Note:

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]$

[[email protected] green]$ ..4

[[email protected] red]$

Note:

Use ‘pwd’ command to check the path :

[[email protected] red]$ pwd

 

/home/linuxteck/red

Note:

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?

Note:

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”.

[[email protected] linuxteck]# cd ~john

[[email protected] john]#

Note:

Use the ‘pwd’ command to check the path:

[[email protected] 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!

About John Gomez

John Britto Founder & Cheif-Editor @LinuxTeck. A Computer Geek and Linux Intellectual having more than 10+ years of experience in Linux and Open Source technologies.

View all posts by John Gomez →

Leave a Reply

Your email address will not be published. Required fields are marked *

L