Thursday, 20 October 2011

Working with directories


Once we are in a terminal in a Linux setting, it is no longer a point and click interface for making folders or moving files into folders. In a Windows setting, it has a graphical user interface (GUI) and it is easy to visualize where the folders are and is easy to move folders around. They use the term folder because it comes from putting files into folders.

In a Linux setting, we use the term directory which has exactly the same meaning as folder. In a Linux setting, there is a GUI but terminals are used more often. Furthermore, once we ssh into another machine, we only have a terminal. Therefore, it is useful to know how to make directories and how to move between directories in a terminal.

As mentioned in the previous post, making a directory is fairly simple which is mkdir name (ex. mkdir Project). However, we need to consider where we want to create before making one. A question arises how to get there.

For example, in your machine, you have the following directory structure.

  • /
    • home
      • Jinny
Suppose you are in the root directory and you want to go to the home directory. You can simply do cd home because the home directory is right below the root directory. Now, you are inside the home directory. If you want to make another directory called Guest inside home directory, then you can do the following: mkdir Guest. It will look like

  • /
    • home
      • Jinny
      • Guest
After making the Guest directory, you can get into the Guest directory by using the cd command (cd Guest). You can use cd .. if you want to go back to the parent directory of Guest which is home. The command cd .. brings you back to the parent directory. There is another command cd ../.. at which the first ../ brings you one level up and the second .. brings you another level up. For example, you are inside the Guest directory and you want to go to the root directory. Using cd ../.. will brings you back to the root directory.

There are two types of paths we can consider when moving around: absolute path and relative path. Absolute path always starts from the root to where you want to go and it doesn’t matter where you are currently located. For example, if you are inside the Jinny directory and you want to be in the Guest directory, you can use an absolute path and type cd /home/Guest. One advantage of the absolute paths is you don’t need to know where you are now. From anywhere, the absolute path of the destination is always the same. By the way, if you want to check where you are, you can use the command pwd which shows the path of the working directory.

Contrast to the absolute path, the concept of relative path is where the destination directory is relative to where I am right now. For example, if you are inside the Jinny directory and you want to go to the Guest directory, you can use relative path and type cd ../Guest. The first ../ makes you get out of the Jinny directory and the second Guest makes you go into the Guest directory. One advantage of relative paths is you can type less. However, you need to think about how the destination is related to where I am now.

No comments:

Post a Comment