WriteBash - 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
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:
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.
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:
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.
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.
If you appreciate what we share in this blog, you can support us by:
- Stay connected to: Facebook | Twitter | Google Plus | YouTube
- Subscribe email to recieve new posts from us: Sign up now.
- Start your own blog with SSD VPS - Free Let's Encrypt SSL ($2.5/month).
- Become a Supporter - Make a contribution via PayPal.
- Support us by purchasing Ribbon Lite Child theme being using on this website.
We are thankful for your support.