Linux Filesystem

One of the first thing you need to understand about GNU/Linux is its filesystem. Linux Filesystem follows the UNIX convention and its pretty straightforward. But it still needs some understanding if you are new to Linux, specially if you have been accustomed to Windows for some time.

(Please note that by filesystem I mean the way Linux paths are arranged and the files are handled. It's not an article about the specific filesystems (e.g. ext4, FAT etc.) on disk.)

In the very basic Linux Filesystem has a single tree structure. There is a single root and all the files and folders are branched from it. The storage devices, instead of having a separate tree structure (like separate drive letters in Windows) are attached to it as branches like the other folders.

In Linux everything is a file

It may sound confusing but actually its a pretty straightforward concept. All the data files, devices, processes etc. are treated as files and users may interact with them as such, which greatly simplifies things.

Suppose you want to clone a storage device. You can simply copy the file designating the device to your target. You can even use a file as a storage device (virtual disk) without needing anything special. It's as simple as that.

All files and folders are part of the same hierarchy of Linux Filesystem

Linux Filesystem Tree
Figure: Example of Linux Filesystem tree

As the image shows there is only a single root directory. All files and folders are its branches.

The devices are also considered are to be a part of this hierarchy. To access a device we need to attach it to a folder which acts as sort of a gateway. This process is known as mounting. We will come to it shortly.

Path

A path is the notation used to indicate the location of a file or folder in Linux Filesystem. A path can be of two types: absolute and relative.

Absolute path is the full path of a file starting from the root directory. For example the absolute path of the file.txt file in the above image will be—

/home/user-1/folder-1/file.txt

Relative path is the path of a file or folder in respect to the current directory the user is in. So if you are currently working in the /home/user-1 folder, then the relative path for the above file will be—

folder-1/file.txt

Notice that the its the absolute path with the path of the current folder removed from it.

Sometimes to distinguish the path of a folder from that of a file a / is added to the end of the path. For example—

/home/user-1/folder-1/

But it isn't strictly necessary.

Mount

Mounting is the Linux way of opening a storage device for access.

Mounting a Device in a Linux Filesystem
Figure: Mounting a device

Suppose you want to open your Pen-drive in your Linux computer. It involves two steps.

  1. First you connect your pen-drive to one of the USB ports. It creates an entry under the /dev directory designating your pen-drive, probably something like /dev/sdb1. But you can't yet access the files within it. For that you need the next step.
  2. Next you mount the device (/dev/sdb1) to a folder of your choice. You can mount a device to virtually any folder, even non-empty ones. At this point you can access the files on your pen-drive by simply opening the folder where you mounted it.

For example you can mount the device to a folder named /mnt/pendrive.

In Linux mounting can be done by issuing the command mount <device name> <target folder>. So in the above example the command will be —

mount /dev/sdb1 /mnt/pendrive

(Please note that mounting a device involves user permission which we will cover shortly)

Now after you have mounted the device in the Linux Filesystem you can see its content by opening the /mnt/pendrive folder.

Permissions

Probably the best thing about Linux Filesystem is its file permissions. Every file and folder has strict rules mentioning which user on the system can view or modify it.

Linux is a multiuser operating system. The root is the supreme user of a Linux system, that can do absolutely anything. That means it's not usually safe to use the system as root, as it can lead to accidental modification of system files, or may run malicious programs. That's why usually a user logs in to a system as a non-root user, and when he/she needs to run any privileged command he/she elevates the access level by prepending the command with sudo, e.g. —

sudo mount /dev/sdb1 /mnt/pendrive

Now every file in a Linux Filesystem has two ownership information: user and group (a group is a collection of users). That means there can be three types of people who might try to access a file —

  1. The user who owns the file (user)
  2. The user group the file belongs to (group)
  3. Anyone else who is not part of the group (others)

In Linux Filesystem there can be three modes of file access —

  1. Read (users can read the content of the file, not modify it)
  2. Write (users can modify the file)
  3. Execute (users can run the file as a program)

So there can be 3×3 equals a total of 9 combinations. These combinations can be marked by 9 contiguous letters, first three for the user, middle three for the group and the last three for others. Suppose a file has the following permissions— rwxr-x--x. It means that the owner of the file has all permissions for the file, the group can only read and execute the file but not modify it, and others can only execute the file.

In Linux you can view the permissions of a file by issuing the command ls -l <filename>.

Links

Links are the files that point to another file. There are two kinds of links in Linux Filesystem—

Soft Links also known as Symbolic Links: They point to a file in another location. A symbolic link can span across different drive.

Hard Links: Hard links are like a duplicate entry of the same file. It can only be used within the same drive.

Before finishing

Linux Filesystem is simple. Yet it needs a little understanding in order to be fluent with Linux. This article is far from being exhaustive, but we have covered the bare essentials of understanding how Linux handles the filesystem. Being familiar with any system requires effort and patience. So it's recommended that you try out some of the file operations yourself to be more comfortable around a Linux box.

Some useful links

Linux Filesystem permissions on Wikipedia

Tutorial for Linux commands

Comments

Copyright © 2012-2020 Dr. Agnibho Mondal
E-mail: mondal@agnibho.com