change directory command
Tags: linux, terminal, bash, beginners
Published: 2021-06-21 04:41:49

This is my first blog on this platform. 😁
I have written this post keeping non linux users in mind. Just to make sure that they understand it clearly. Let's start the linux journey. 

We might face the situation where we have to navigate the file system through a terminal or cmd because of lack of GUI  feature in the system like Servers. So it is must to know the basics of `cd` command.

cd command means Change Directory which is kind of self explanatory term. Now before jumping into the examples there is something we should know i.e, `Paths` in file system.

There are two types of paths in any operating system. 
1. Absolute Path
2. Relative Path

### Absolute Path
Absolute path is nothing but a path given from the root of the file system.
```
/home/user/Desktop/someDirectory
```
This is an example of absolute path because we have to provide the path from the bottom most part of the file system heirarchy.

### Relative Path
In Relative path we don't have to give whole location of the file. Let's look at the relative path with same example
```
~/Desktop/someDirectory
```
 Noticed the difference ? The ~ symbol denotes the present working directory (`pwd`). If we execute 
```
pwd
```
we would get a file path like
```
/home/user/Desktop
```
Now if I want to navigate to someDirectory folder I can use
```
cd someDirectory
```
Notice that we haven't used `/` which indicates it's a relative directory to present working directory.

So just keep in mind if you are going to navigate into the files outside of your pwd you have to provide the full path AKA Absolute path otherwise you can use the short path AKA Relative path.
Now its time for a hands on.

## Examples

### Navigate to Desktop
```
cd ~/Desktop
```
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/klaayyrrmujy6z4lfls9.png)
 

Let's create a file in Desktop with `mkdir` command. 
_[Note : I will write a dedicated blog post for file operation commands in my second blog where i will cover this touch command, for now just know that mkdir command is used to create a empty file in the system]._

```
mkdir thisFile.txt
```
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aszqfayqejbnwoows8yu.png)
Now if you notice the path near the time you will see its a relative path `~/Desktop/someDirectory` since we are working in the user space. 

### Navigating back using cd
Now lets say we want to navigate back to the user space from the `~/Desktop/someDirectory` folder. we will use cd with two dots i.e `cd ..`
```
cd ..
```
This will take us to `~/Desktop` folder only. If we want to go further back we can use the same cd ..
```
cd ..
```
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/izds3n6lyeyigqrax284.png)
But this can be tiring hence we have a shortcut for it. you can concat the dots the number of times you want to go back.

for example.  We wanted to go to `~` from `~/Desktop/someDirectory` which means two jumps back so we can use this command
```
cd ../../
```
This will directly take us to the ~ folder
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/duo7cwowqdx58gfbx9c0.png)
 If there were three jumps then it would be `cd ../../../` and similar things for other jumps.

But wait we can make this even more shorter. If our goal is to reach the ~ folder we can simply use 
```
cd ~
```
Why does this works ? if you print the `~` symbol you will get your user space.

```
echo ~
```
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xuczmctzeuc5uastogkn.png)
 

`echo` command is similar like print statements in programming languages.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ic8cs78zl2c655gda43f.png)
 
This only works if we want to go to ~ whereas `../../` method is more flexible to use and is mostly used when compared with `cd ~`.
Now if you are in the user space then you don't have to provide `~` with cd i.e to navigate to Desktop folder you can use 
```
cd Desktop/someDirectory
```
instead of 
```
cd ~/Desktop/someDirectory
```
But what if we want to navigate from a Downloads folder to Desktop/someDirectory ?
Assume that we are already in Downloads folder.

First Case, we can use
```
cd ..
cd Desktop/someDirectory
```
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nok3x1noj4qu02lfdhh3.png)

Second Case, here's why we use ~ in front of relative paths. We can achieve the same result using 
```
cd ~/Desktop/someDirectory
``` 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j9mv2kc3ytrk8khod18v.png)
Now it might be easy to visualize what `~` is right ? As i said earlier it is nothing but `/home/user` hence our
```
cd ~/Desktop/someDirectory
```
is actually
```
cd /home/user/Desktop/someDirectory
```
This is why we don't need to use `cd ..` like in first case.



### Navigating in System space

System space is nothing but folders that are used by our system for its own purpose. We will cover this in future.
For now let's keep this simple, anything outside of ~ or `/home/user/` is system space.

There is boot folder in linux which contains all files related to booting the system. To navigate to that folder we have to provide the full path
```
cd /boot
```
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ajasj50bi4arl06km0wi.png)
Noticed something ? The path near the time changed to absolute path `/boot`. 
This is the only difference between a relative path and absolute path. All the commands discussed for relative paths work similarly in absolute path as well.