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:
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 ??.
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
领英推荐
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
As a last piece of info, you can check how many hard links exist for one file by running:
ls -l
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:
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