Learn Shells and Terminals

Learn Shells and Terminals

This is part 2 of this series. You can find part 1 here. This article will discuss file system in Linux. Have fun reading :)

1. Filesystem

The data in your computer is organized into files and directories, and they are organized into a tree-like structure, which is called a filesystem. The filesystem starts with a single directory called root directory. The root can contain more files and directories, and so on.

When you open your terminal, you're going to be somewhere, and you want to determine where you're at on the filesystem. Run the command "pwd" and you can see the filepath of your current working directory:

pwd        

The output of this command is a filepath. You should see something like:

/home/quck        

The result will be different, but the structure should be the same.

  • The first slash "/" is the root directory.
  • The word "home" is the name of one directory under root, "quck" is one directory under "home" and so on.

2. Directories and moving around

You can see what's inside your current directories by using the "list" command. My result shows that I have 3 directories under my current directory:

ls
# Downloads  go  workspace        

If you want to move to any directory, you can use the command "cd", which stands for change directory, plus the path to the directory you want to go to. For example, I will go to "workspace" and will check if I really go into it:

cd workspace
pwd
# /home/quck/workspace        

Now you know how to move into a directory, how about going back to the parent directory? You will use two dots: "..", a special directory name that translated to "parent directory". You can use it to move up one level in the directory tree. I have gone back to "quck" directory, the parent of "workspace" one after using the commands:

cd ..
pwd
# /home/quck        

You can also going upward or downward multiple times in one command by adding more path in the "cd" command. Going upward 2 directories can be achieved by running:

cd ../..        

Or moving into directories by:

cd workspace/linux-tutorial
pwd
# /home/quck/workspace/linux-tutorial        

3. Absolute path and relative path

Remember when I say the argument for "cd" command is the path of the directory you want to move to. That's why you need to understand file path in Linux thoroughly so that you can move freely in your Linux machine. Until now, we've mostly been dealing with relative paths, they are paths that take into account the current directory.

Let's say we have the following directory structure in the filesystem:

animals
|--- poultry
|         |--- birds
|         |           |--- chicken.txt
|         |           |--- turkey.txt        

When inside the "animals" directory, the relative path to the chicken.txt is:

 poultry/birds/chicken.txt        

However, if we're in the "birds" directory, the relative path is just:

chicken.txt        

The relative path is short and convenient, and we use it most of the time.

How about absolute path? Absolute path is a path that starts at the root of the system. When I break down the structure of a filepath in section 1, I mentioned the root is denoted by a forward slash "/". So if the "animals" directory is in the filesystem root, the absolute path to the chicken.txt is:

/animals/poultry/birds/chicken.txt        

So, when inside the "birds" directory, you can use either:

chicken.txt        

or

/animals/poultry/birds/chicken.txt        

to refer to the same file.

Now you may say absolute path is unnecessary because relative path is shorter and way more convenient. Well, it depends. Relative paths are easier to read and write, and as long as you're in the correct directory (or the directory you expect), they're easier to reason about.

Absolute paths are more explicit. They're useful when you're not sure what directory you're currently in. For example, maybe you're giving someone instructions on how to find a file on their computer. You can't be sure what directory they'll be in when they start following your instructions, so you'll need to use an absolute path.

Now it's up to you to practice and to make yourself comfortable with linux filesystem.


This is the end of this article, it's part 2 of this learning shells and terminal series, I hope you like it. If you find it useful, please hit that like button to support me. Thank you for your time :)))

Hay a ơi

Like
Reply

To view or add a comment, sign in

More articles by Quoc Khanh Cao

  • Learn Shells and Terminals

    As developer, you'll most likely work with command line a lot. I intended to write a series about command line, so I…

    4 Comments

Insights from the community

Others also viewed

Explore topics