Command to change directory in Linux
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:
You Might Be Interested In
- Create links with ln command
- Pathnames types in Linux systems
- Remove files and directories with rm command
- Type command to display a command’s type
pwd
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.
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 ..
The second method uses absolute pathname.
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”.
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).
Original post: Command to change directory in Linux — DevOps Lite