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.
- Absolute Path
- 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
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
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 ..
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
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 ~
echo
command is similar like print statements in programming languages.
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
Second Case, here's why we use ~ in front of relative paths. We can achieve the same result using
cd ~/Desktop/someDirectory
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
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.
Top comments (2)
That's not quite right. The path
~/foo/bar
is an absolute one, your shell is expanding~
to your home directory on the fly.A relative path is one relative to your current working directory. So if you're in
/home/user
and you saycd /tmp
you'll end up in/tmp
(absolute) but if you saycd tmp
without the root, you'll end up inhome/user/tmp
, assuming it exists.You can tell that paths starting with '~' are absolute because it doesn't matter where you are when you use them.
Got it ! Thanks for pointing out.