Moving folder with an arrow symbolizing the files and folder in Linux and their structure within it as well as how to change directories.

How to Change Directories in Linux – CD Command Examples

Hey There! Welcome!

Welcome to this lesson on navigating the Linux terminal! Today, we’ll master the art of accessing any directory with ease and confidence. So let’s get started!

Change Directories with Cd:

In our previous lesson, we discovered that the terminal is always focused on a specific directory, allowing us to effortlessly interact with the files it contains. By default, this is our user’s home directory.

But what If we want to view or interact with files in another directory? No worries – the terminal has us covered. With a simple command, we can teleport our terminal to any directory we want.

To demonstrate, let’s say we want to navigate to the root directory. To do this, we need to use the cd command, which is short for “change directory.” Next to it, we need to specify the path of the directory we want to navigate into. As we learned in our previous lesson, the root directory is represented by a single slash instead of its actual name. Let’s give it a spin!

				
					ubuntu@ip-172-31-45-72:~$ cd /
ubuntu@ip-172-31-45-72:/$ 
				
			
To verify our success, we can employ the pwd command, just like we learned in our previous lesson.
				
					ubuntu@ip-172-31-45-72:/$ pwd
/
ubuntu@ip-172-31-45-72:/$ 
				
			

Success indeed! The single slash confirms that we’re now in the root directory.

Taking a look around with ls:

Now that we’ve arrived, let’s take a look around using the ls command.

				
					ubuntu@ip-172-31-45-72:/$ ls
bin   dev  home  lib32  libx32      media  opt   root  sbin  srv       sys  usr
boot  etc  lib   lib64  lost+found  mnt    proc  run   snap  swapfile  tmp  var
ubuntu@ip-172-31-45-72:/$ 

				
			

Awesome! The contents of our current directory are now on display.

If you are following along with us you will notice that the names of the directories have different colors on your terminal. We’ll delve deeper into what these colors signify in the next lesson but for now all you need to know is that blue is for directories, while other colors represent various types of files.

So, what exactly are we looking at here? The folders within our system are meticulously organized based on the items they contain and their respective purpose or function.

For example, you might recognize the home directory which contains each user’s personal home directory. We can see several other directories as well, but let’s save those for the next lesson as well.

Practice exercise: Navigating to the /home directory:

Now that we know the fundamentals of changing directories within the command line, let’s put our skills to the test by diving into the home directory we just discussed. Can you figure out the command we need to use? Put on your thinking cap! Ready, set, go!

Here’s the answer: First, we need to use the cd (change directory) command, followed by the path of the directory we want to navigate into.

Since the home directory is located within the root directory, its path is simply a slash, which represents the root directory, followed by the word home. Easy as pie, right?

Just remember, the Linux terminal is case-sensitive, so make sure you type home with a lowercase h! With our command ready, let’s hit that enter key.

				
					ubuntu@ip-172-31-45-72:/$ cd /home
ubuntu@ip-172-31-45-72:/home$ 
				
			

To figure out if we were successful, we need to use the pwd command once again.

But wait — take a look at the command prompt, specifically the text following the colon punctuation mark. The terminal conveniently displays the path of the directory we’re currently in.

This handy feature allows us to effortlessly identify our current directory without resorting to the pwd command. In this case, it confirms that we’ve successfully navigated to the /home directory. How cool is that?

Change Directories Using Relative & Absolute Paths:

Lets put our new found skills to the test one last time. Lets begin by taking a look at the contents of our current directory using the ls command:

				
					ubuntu@ip-172-31-45-72:/home$ ls
ubuntu
ubuntu@ip-172-31-45-72:/home$ 
				
			

As we touched upon in our previous lesson, Linux automatically generates a directory for each computer user, named after their username, within the /home directory. This structure ensures that each user’s files are organized and kept separate (more about this magical directory here). In our case, there is only one user, and the directory named after it is known as the home directory for this specific user. Let’s step inside!

As always, we need to start with the cd (change directory) command followed by a slash to represent the root, since that is where the home directory is located. Directly after, let’s add the home directory itself, along with another slash and the name of our user’s home directory.

				
					ubuntu@ip-172-31-45-72:/home$ cd /home/ubuntu
				
			

While this command would work perfectly well, there’s a nifty trick we can use to save ourselves from all that tedious typing.

Up until now, we’ve been using full or absolute paths, which describe an item’s location starting from the root directory all the way to the item itself. However, in this case, we can use something called a relative path instead.

Relative paths describe an item’s location not from the root, but from our current directory. Lets see what they look like:

				
					ubuntu@ip-172-31-45-72:/home$ cd ubuntu
				
			

To create a relative path, all we need to do is subtract our current working directory (/home) from our target path (/home/ubuntu). We can also omit the slash since we don’t need a separator. This leaves us with just the name of the directory we want to access (ubuntu). Pretty cool, right?

In practice, this means that whenever we need to interact with files in our current directory, we can simply use their names rather than their full paths – no more typing marathons!

It’s important to note that the terminal’s ability to focus on or associate with a specific directory is what enables the use of relative paths. By providing this context, the terminal allows us to exclude our current directory from an item’s path. If the terminal didn’t have this capability, we’d be forced to use absolute paths all the time. Talk about a nightmare!

Having expressed our gratitude to the terminal, let’s go ahead and execute our command.

				
					ubuntu@ip-172-31-45-72:/home$ cd ubuntu
ubuntu@ip-172-31-45-72:~$ 
				
			

To determine whether we were successful, we can simply glance at our command prompt, just as before.

What do we have here? Instead of our current directory, the tilde mark is displayed. That’s because this squiggly line is an indicator that we’re located in our user’s home directory.

In other words, it represents our user’s home directory in a shorter and more concise manner. This means that we’ve successfully arrived at our destination!

Final Thoughts:

Remember, navigating directories in the terminal can feel a bit like learning to ride a bike at first – it might seem daunting, but with practice, it’ll soon feel natural and effortless.

Practice Time:

Homework: Take a field trip to your root directory. Use cd / to start, and explore with both relative and absolute paths.

Tips & Tricks:

  • Shortcut to Home: From anywhere, cd with no arguments takes you back home. No place like home, right?

  • Autocomplete: Feeling lazy? Type the first few letters of a directory and hit Tab for the terminal to finish it for you.

Fun Facts to brighten your day:

  • Terminal Time Travel: Using cd .. to go back a directory is like time traveling to the past, but without the risk of changing history. Just don’t expect to see dinosaurs!

  • Root Directory – Not a Plant: In Linux, the root directory is the top-level directory. No watering needed, and thankfully, it doesn’t grow weeds!

  • Home Sweet /home: The /home directory is like the living room of your Linux system. Cozy, personal, and no need to take off your shoes.

  • cd – The Teleporter Command: If only changing locations in real life was as easy as typing cd. Want to go to Paris? Sorry, you’ll need more than a terminal for that!

  • The Relative Path Shortcut: Using relative paths in Linux is like finding a shortcut in a maze. Less typing, more doing!

Test Your Knowledge (Practice Quiz):

There has been error loading this quiz, please refresh the page or enable JavaScript to continue.

0/5

Take your Knowledge to the next level!

Learn the Linux command line with our interactive course!

Welcome back!

Don't have an account? Select the register button to create one!

First time around?

You're in the right place! Fill out the form to create your account: