Everything is a file in UNIX ?
Tags: unix, beginners
Published: 2022-06-11 21:20:57


You most likely heard that _everything in UNIX is a file_, but it never really clicked for you. Or you get it, but couldn't find the technical details behind it. Or you have no clues about what this even means - _like me before this_.

In any cases, you are at the right place. Got your coffee ? ☕ Let's dive in.

## Table of contents

-   [Is everything really a file ?](#everything-file)
-   [What is a file, if everything is a file ?](#what-are-they)
-   [Inode](#inode)
    -   [Basics](#basics)
    -   [Inode number](#inode-number)
-   [Directories](#directories)
-   [Data](#data)

## Is everything really a file ?

<a name="everything-file"></a>

Yup.

This yet simple but quite complex sentence resume the entire architecture of the UNIX Operating System (**OS**).

Basically, every features your OS is able to do, rely on files. Processes, devices (such as a printer, or your mouse), networks, and even directories.

_Wait, directories ?_

Yup. There are 7 different types of files, you can find a complete list [here](https://en.wikipedia.org/wiki/Unix_file_types). For now, we'll focus on regular files, and directories.

## What is a file, if everything is a file ?

<a name="what-are-they"></a>

From an OS point of view, a _file_ is actually nothing more than a specific data structure called an **inode**. You may visualize an inode such as a table containaing file related informations.

The inode itself contain all the metadatas (extra datas that describes data) relatives to the file it describe. Informations such as the creation date, the last update date, the ownership, the permissions, the file size ...

---

⚠ _The file name and the actual file data are not part of the inode, keep it in mind, we'll cover this later._

---

### Inode

<a name="inode"></a>

#### Basics

<a name="basics"></a>

You can visualize an inode informations directly in your terminal, by typing the `ls -l` command.

Here's the kind of output you should receive :

```bash
-rw-r--r--  1 user  root  13 10 mai 22:10 index.txt
```

In order :

-   File type and permissions. It's quite hard to read, let's break it down together.

    -   The first character specify the _what kind_ of file this is (the 7 types we mentionned earlier).
        -   **-** Regular file.
        -   **d** Directory.
        -   **l** Symbolic link.
        -   **b** Block special file.
        -   **c** Character special file.
        -   **s** Socket link.
        -   **p** FIFO.
    -   The next three characters are related to the _owner_ permissions for the file.
    -   The next three characters are related to the _group_ permissions for the file.
    -   The last three characters are related to the _others_ permissions for the file.

        All permissions fields can be read as follow : 
        - Is the _permission_ allowed to **read** the file ? **-** for no, **r** for yes. 
        - Is the _permission_ allowed to **write** the file ? **-** for no, **w** for yes. 
        - Is the _permission_ allowed to **execute** the file ? **-** for no, **x** for yes.

-   Number of hard links. You may find more informations [here](https://www.geeksforgeeks.org/soft-hard-links-unixlinux/#:~:text=A%20link%20in%20UNIX%20is,to%20the%20same%20file%2C%20elsewhere.).
-   Owner name.
-   Group name.
-   Number of bytes in your file.
-   Date of last modification.
-   ~~File name~~. _Not a part of the inode, but still in the output. More on it later._

---

📌 _You may find all these informations - and more, directly from your terminal in an UNIX environnement with the command `man ls`_

---

If you're willing to get a bit more informations about a specific file, you may want to use the `stat` command directly.

#### Inode number

<a name="inode-number"></a>

If you execute the `ls -li` command you will find an extra column at the beginning of the output.

```bash
12345678 -rw-r--r--  1 user  root  13 10 mai 22:10 index.txt
```

It contain a very specific integer, the **inode number**.

💡 **This number is the unique identifier for this specific file in your entire file system.**

Every files will have a different inode number (with the exception of [hard links](https://www.geeksforgeeks.org/soft-hard-links-unixlinux/)), and there's a maximum amount of inode number your file system can handle. Afterward, you will not be able to create anymore files.

### File name and directories

<a name="directories"></a>

If the filename is not specified by the inode, where is it stored ?

You most likely already guessed it by the title, but the file name is stored within the **parent directory**.

What exactly is a directory then ? It's actually a pretty simple thing. Type `ls -l` command somewhere with a directory.

```bash
drw-r--r--  1 user  root  13 10 mai 23:17 directoryName
```

_Noticed the **d** at the beginning ?_

The directories files actually only contain a mapping table, between a file inode number, and his name.

| Inode number | File name |
| ------------ | --------- |
| 900          | file.txt  |
| 901          | data.json |

The list goes on for all the file or other directories it may contain.

A directory is nothing but a specific file, it also have an inode number, and his name is saved within his parent directory inode.

---

📌 _`ls -li` for a directory to see the inode number_

```bash
899 drw-r--r--  1 user  root  13 10 mai 23:17 directoryName
```

Here's a complete representation of the content of a directory.

💡 **The first two entries are references to itself, and to his parent directory.**
📌 _To see them, use the `-a` option on the `ls` command to see hidden files._

| Inode number | File name    |
| ------------ | ------------ |
| 898          | .            |
| 899          | ..           |
| 900          | file.txt     |
| 901          | data.json    |
| 902          | newDirectory |

---

### The file data

<a name="data"></a>

So, where is the file data ?

What make the inode so special is that it kept references (**pointers**) toward the memory blocks that are actually containing the data in disk. By doing so, when we ask to open the file, it go through all of them and recover the informations needed.

*The pointers part is oversimplified for the sake of this article, indirect pointers are purposely omitted.*

Let's get a visual recap for a _file_ inode.

| File Inode                          |
| ----------------------------------- |
| File types and permissions          |
| Hard links                          |
| Owner name                          |
| Group name                          |
| Number of bytes                     |
| (A lot more metadata informations ) |
| Pointer toward block n° 100         |
| Pointer toward block n° 101         |


---

I hope you enjoyed the read, and learned as much as I did. If you have any questions or comments, feel free to reach to me on [Twitter](https://twitter.com/ChandelierAxel) or in the comments below. Have a nice day !