Linux File Links
CREDITS: Some illustrations here are made by Freepik (https://www.freepik.com)

Linux File Links

It's been on my mind for a while to start writing random technical stuff. And, since I'm starting to really really miss working with Linux (the downside for working with classic AUTOSAR on the long run ??), I decided to write my first piece about Linux file links. Yeah, very specific ??

Let us first start by describing how Linux "understands" files. Generally, there are 2 parts for each file:

  1. The actual content (data blocks).
  2. Metadata about the file (inodes): This includes stuff like file type, permissions, size, and some info that should point (directly or indirectly) to the location of the data blocks on the disk.

Each inode has a unique number per filesystem, called the inode number. One thing that is not considered a metadata here is the file name/path. This is not a part of the information held within the inode structure. However, a directory takes care of this little piece of info.

You can imagine a Linux directory as a specially formatted file which contains a table of file names with their respective inode numbers.

Hint: try

ls -i        


Here comes the famous mandatory quote: Everything in Linux is a file ??.


No alt text provided for this image
Journey from a directory to file content

Enough about inodes. Let's explore the topic of this article: File Links. So, what are those?

A file link is typically a reference to the actual file (perhaps out of reach in a jungle of long paths). That's it: a fancy shortcut.

I'm calling it fancy because unlike Windows shortcuts - which are regular files with a special type (.lnk extension in Windows) treated in a special way by Windows Explorer (an application: explorer.exe), a Linux link acts as the actual file, and is translated on the filesystem level.

We have 2 types of links:

Symbolic (Soft) Links

SymLinks use file names as their reference. So, just like shortcuts, they are useless if the original file gets renamed/deleted.

This is created by running:


ln -s original_file_path sym_link_path        


No alt text provided for this image
A symbolic link

Hard Links

On the other hand, hard links use inodes as their reference. So, they are still usable even if the original file gets renamed/deleted. You may even say that the original file itself is a hard link.

A hard link is created by running:


ln original_file_path hard_link_path        


No alt text provided for this image
A hard link

As a last piece of info, you can check how many hard links exist for one file by running:


ls -l        
No alt text provided for this image
pic1 and pic1_hardlink are both hard links to the same inode, hence we can see '2'

See how d1 (the sub-directory) also have '2' written there? A cool fact here is: when a directory is created, it has at least 2 hard links:

  1. Its "name" in its parent directory
  2. . (dot)

In case it has a sub-directory, another hard link is created which is .. (double dots).


That's all about file links. I would appreciate any contributions regarding this topic (more random info/cool tricks/corrections maybe?).


References:

[1] Everything You Ever Wanted to Know About inodes on Linux by Dave McKay

[2] Hard links, symbolic links, and…inodes? by Mikaela Gurney

[3] Understanding Linux Links by Jack Wallen

要查看或添加评论,请登录

Mariam El-Shakafi的更多文章

  • Inside The Mind of C++ Atomics

    Inside The Mind of C++ Atomics

    We are but a few atoms in the boundless universe. I always cherish this fact with every failure, misunderstanding or…

    4 条评论
  • Transport Layer Security Overview

    Transport Layer Security Overview

    Credits: Image by rawpixel.com on Freepik I am getting security vibes today.

    2 条评论
  • Journey to The Center of.. A Pipe!

    Journey to The Center of.. A Pipe!

    The WiFi on my Ubuntu suddenly stopped showing up. I am not an IT expert, so I immediately ran out of solutions when my…

    2 条评论

社区洞察

其他会员也浏览了