Linux Directory Structure

In Linux, most of the operations are performed on files, for example, text files or images. Directories (folders) are used to help you organize your files. Think of directories like folders in a file cabinet. They have names, just like files, but their function is to contain other files, and other directories. In this way, you can keep the files on your system separate and sorted according to their function or purpose.

Rules for Creating File Names in Linux

  • File Names in Linux are case sensitive. Hence, "File1" and "file1" would represent different files.

  • File names that begin with a period (".file1") are hidden.

  • Don’t use spaces.

  • Limit punctuation to periods, dashes, and underscores.

directory-structure.jpg

Root, Parent, and Subdirectories

The directory at the highest level of hierarchy in a file system is called the root directory. The root directory contains all other folders and files. Every directory, except the root directory, lies beneath another directory. The directory containing the current directory is called the parent directory, and the directory located within the current directory is called a subdirectory. Subdirectories branch off the root of the directory tree. Unlike a real tree, directory trees are upside down. Hence, the root is at the top and the branches reach down. When you move into a subdirectory, you are moving down the tree. When you move into a directory’s parent directory, you are moving up the tree.

root-directory.jpg

By default, when you open a terminal and begin using the command line, you are placed in your home directory.

Absolute Path

It’s the path that indicates the position of a file or directory with respect to the root directory. The absolute path of the root directory is a forward slash ( / ). It starts at the root directory ( / ) and works down. To check the absolute path of any directory, open a terminal and type the pwd command. When the command is invoked, the completed path of the current working directory gets printed. The below example shows the absolute path of the home directory.

Example 1

pwd

Output

/home/<your-username>

Example 2

Consider that the home directory contains the following files and subdirectories.

sample-scripts      text.txt
  • If you want to open test.txt from the home directory, simply type the following command.

cat test.txt
  • On the other hand, if your current working directory isn’t the home directory and you want to open the test.txt file, then you need to specify the absolute path to the file to display it.

cat /home/<your-username>/test.txt

Relative Path

It’s the path which is related to the current working directory. It starts at your current directory and never starts with a /. Consider the following example.

relative path

Example

In the image above, the absolute path for a python directory will be, /home/<username>/sample-scripts/python. Consider that you’re already in the sample-scripts directory and hence the relative path will be python.

Two cryptic symbols are used with relative paths.

Symbols Explanation

. (a single dot)

to represent the current working directory.

.. (two dots)

to represent the parent directory and move up the file hierarchy.

The above symbols can be better understood with the following examples. Consider that you are inside the python directory.

pwd
/home/<username>/sample-scripts/python
  • cd .. helps to go one level up in the file hierarchy.

cd ..
pwd
/home/username/sample-scripts
  • cd . indicates the current working directory and doesn’t move up.

cd .
pwd
/home/username/sample-scripts/python