Command to change directory in Linux

by Daniel Pham
Published: Updated:
This entry is part 1 of 2 in the series Linux commands: Part 2 - Navigation

To work on Linux systems, you have to work with directories and files, in this article I will talk about changing the working directory in Linux using the cd command.

Like Windows, a Unix-like operating system such as Linux organizes its files in what is called a hierarchical directory structure. This means that they are organized in a tree-like pattern of directories (sometimes called folders in other systems), which may contain files and other directories. The first directory in the file system is called the root directory. The root directory contains files and subdirectories, which contain more files and subdirectories and so on and so on.

The current working directory in Linux

As with the Windows operating system, when you work, you need to know that you are currently in the directory in the system. But on Linux (I mean the command line interface when using Linux on the server) there is no graphical interface to let you know that. So how do you know which directory you are in?

To display information about the current directory, use the following command:

pwd
display current directory with pwd command
Display current directory with pwd command

Use “cd” command to change directory in Linux

To change your working directory (where we are standing in our tree-shaped maze) we use the cd command. To do this, type cd followed by the pathname of the desired working directory. A pathname is the route we take along the branches of the tree to get to the directory we want. Pathnames can be specified in one of two different ways; as absolute pathnames or as relative pathnames. Let’s deal with absolute pathnames first.

use cd command to change directory
Use cd command to change directory

An absolute pathname begins with the root directory and follows the tree branch by branch until the path to the desired directory or file is completed.

cd /etc/sysconfig

For example, in the image above, we will move to the /etc/sysconfig directory. What this means is, we start with the root directory (represented by the leading slash in the pathname) there is a directory called etc which contains a directory called sysconfig. You can read more about the ls command in the image above.

Changes from subdirectory to parent directory

We have two methods to do this.

The first method uses a relative pathname. This is the most common method because it “type less”. We use command below:

cd ..
move from subdirectory to parent method 1
Move from subdirectory to parent (method 1)

The second method uses absolute pathname.

move from subdirectory to parent method 2
Move from subdirectory to parent (method 2)

Changes from parent directory to subdirectory

We have three methods to do this. The first method uses the absolute pathname. The second method uses a relative name path. The third method is “use subdirectory name”.

move from parent to subdirectory
Move from parent to subdirectory

The third method is the second method of removing the ./ at the beginning of the command. This is the most common method in the 3 methods above.

Conclusion

In the article I talked about how to use the cd command to change the directory on Linux, this is one of the more useful commands later when you start programming the bash script.

(This is an article from my old blog that has been inactive for a long time, I don’t want to throw it away so I will keep it and hope it helps someone).

0 0 votes
Article Rating
Series NavigationNext part: Pathnames types in Linux systems »»

You may also like

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

DevOps Lite is a personal blog specializing in technology with main topics about DevOps, DevSecOps, SRE and System Administrator. Articles are shared for free and contributed to the community.

SUPPORT US

FOLLOW US

Subscribe my Newsletter for new blog posts. Stay updated from your inbox!

© 2021-2024 DevOpsLite.com – All rights reserved.

Please write sources “DevOpsLite.com” when using articles from this website.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

2
0
Would love your thoughts, please comment.x
()
x

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.