2) [ Linux commands series ] cd

2) [ Linux commands series ] cd

An overview of cd command with examples

ยท

4 min read

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]
  1. Square brackets [] mean that the section is optional and can be omitted.
  2. [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: Screenshot from 2022-10-04 21-56-19.png Nothing happened? - Yes, exactly. Let me explain why:

  1. By default a new terminal window opens in home directory (the tilde "~" means "home directory").
  2. 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): l143EHlrP.png 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: CM304b_aA(1).jpg

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": Screenshot from 2022-10-06 20-27-17.png Screenshot from 2022-10-06 20-17-51.png

I continue to move further until I find my videos ๐Ÿ˜Š: L4wwcIykC.jpg 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: XXHVUHkpd.jpg 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  ~/

Screenshot from 2022-10-09 22-19-47.png

Previously I found my videos by using cd multiple times: Screenshot from 2022-10-08 18-46-22.png

But instead of doing it I can reach the same outcome in one line specifying the path to the directory my_new_channel: Screenshot from 2022-10-08 13-12-37.png 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 ๐Ÿฑ: Screenshot from 2022-10-08 19-37-34.png

Again, the work that was done in the screenshot above can be replaced in one line: Screenshot from 2022-10-08 19-39-43.png 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 : cDQYh4gGQ.jpg

 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!

ย