2) [ Linux commands series ] cd
An overview of cd command with examples
Hello dear readers. I continue my Linux commands series. Make sure that you have read my previous blog post about ls
because the command will be used quite often in this article. Today we will be traveling in the file system of Linux. For this purpose, it's necessary to learn how to navigate across directories or how to change directories.
In the article you will learn about cd command.
1. What is cd
about?
The command cd
stands for change directory. I believe that this is self-explanatory name. We use cd
for navigating across directories of file system.
2. Usage of cd
cd [PATH TO THE DIRECTORY]
- Square brackets
[]
mean that the section is optional and can be omitted. [PATH TO THE DIRECTORY]
- in this section you can put the path for the directory where you want to go to.
3. Practice
3.1 Using cd
without options
The theory should be supported by practice, so I will open my terminal and simply type:
cd
the output: Nothing happened? - Yes, exactly. Let me explain why:
- By default a new terminal window opens in home directory (the tilde "~" means "home directory").
- If any option wasn't provided for
cd
then it means "navigate me to home directory".
That's why nothing happened. I'm already in home directory and I'm navigating to the home directory =).
One more thing before we will move further. It's really important to know where you're right know in the file system. Usually, the terminal prints out your current "position" (working directory): It's super helpful, and in our traveling it will be a kind of GPS. ๐
3.2 Using cd
with options
3.2.1 Jumping into directories
OK, I'm in my home directory. Let's use ls from the previous article to check out what we have:
In Linux, file can be without extension. To differentiate a directory from a file you can use -l
option of command ls
. The d
at the beginning of each row points out that we're dealing with directory and we can navigate into it. Let's check out directory "Videos". It's possible to do it in these ways:
cd Videos
or
cd Videos/
or
cd ./Videos # "./" means "the directory or file in the current directory"
or
cd ./Videos/
All the above commands mean only one thing "go to the directory Videos":
I continue to move further until I find my videos ๐:
the -
sign at the beginning of each row indicates that we're dealing with a common file.
Let's use cd
without options to quickly move to the home directory:
By the way, instead of using simple cd
(without options) it's possible to achieve the same result with the following alternatives:
cd ~
or
cd ~/
Previously I found my videos by using cd
multiple times:
But instead of doing it I can reach the same outcome in one line specifying the path to the directory my_new_channel:
As you can see from the screenshot each directory name from the path should be divided by slash /
.
Also, remember these are full equivalents to cd Videos/youtube/my_new_channel
:
cd Videos/youtube/my_new_channel/
cd ./Videos/youtube/my_new_channel
cd ./Videos/youtube/my_new_channel/
3.2.2 Exiting from directories
What if I want to exit from the current directory? In this case cd
with option ..
(2 dots) can be used. In practice it looks like this:
cd ..
or
cd ../
or
cd ./..
or
cd ./../
All the above commands mean "exit from the current directory" and each variant will lead to the same result. Just pick the one you like more ๐ฑ:
Again, the work that was done in the screenshot above can be replaced in one line:
In this case cd ../..
means "exit from the directory my_new_channel then exit from the directory youtube"
One more screenshot to illustrate how we can use multiple ..
expressions in one line :
Finish ๐/my_home_directory <- this is the point of destination after executing cd ../../..
|_๐ Desktop
|_๐ Documents
|_๐ Downloads
|_๐ Music
|_๐ Pictures
|_๐ Public
|_๐ Templates
|_๐ Videos ( 3. then from Videos )
|_๐ funny_cats
|_๐ youtube ( 2. then from youtube )
|_๐ my_old_channel
Start |_๐ my_new_channel ( 1. we have exited from my_new_channel first )
|_๐ฅ 1.mp4
|_๐ฅ 2.mp4
|_๐ฅ 3.mp4
|_๐ฅ 4.mp4
|_๐ฅ 5.mp4
|_๐ฅ 6.mp4
And for the sake of completeness, the full equivalents of cd ../../..
:
cd ../../../
cd ./../../..
cd ./../../../
4. Conclusion
In this article we have observed the usage of cd
. As you saw the process of navigating across file system in Linux is pretty easy and fun. Practice more and you will be confident with cd
quite soon. Thanks for reading!