You’ve just seen what is a hard link and what are its properties and use-cases. 1
Let’s move on and talk about soft links, also called symbolic links or symlinks. 2
They are similar to shortcuts in Windows and behave differently compared to hard links. 3
Let's take a look at this picture. 4
If a hard link is an association between an inode structure and a filename 5
in a directory, a symlink 6
is just a special filetype that points to or contains a reference to another file or directory. 7
So a hard link references the file’s inode and a symlink references another file. 8
If you delete a hard link, the remaining files point to the same space or inode in the file system 9
and they will still contain the data stored on the disk. 10
But if you delete the file, the symlink points to the symlink will be useless or broken. 11
To create a symlink we use the same command as for creating hard links, which is ln, but with the 12
-s option. I'm creating a symlink: ln-s the existing file /etc/passwd and symlink 13
in the current directory ./pswd. 14
I've just created a symlink called pswd to etc/ password. 15
See how ls -l is indicating that pswd is a symlink and it’s also displaying the file 16
it points to. 17
Let's see another example! I'm creating a new file using a redirection. 18
ps aux > processes.txt 19
By the way, a ps aux is displaying all running processes. 20
And the output was redirected to this file. 21
And I'm creating both a hardlink and a symlink to the newly created file, so ln -s processes.txt 22
p.txt This is a hardlink and the now a symlink: ln -s processes.txt 23
and symlink_p.txt 24
I'm running ls-li 25
This is the original file processes.txt 26
This is the hard link, they have the same inode number, this number. 27
And this is another file, the symlink. 28
I can access the contents of processes.txt using the symlink. If I move processes.txt to another 29
directory, p.txt, the hardlink will not be affected, but the symlink will be broken. 30
Let's do it. 31
mv 32
processes.txt 33
And the destination directory, I'm moving the file to my desktop. 34
And cat p.txt the hardlink. 35
And it's working, there is no problem, ls -l is showing that the symlink is broken, 36
the symlink points to nothing, cat symlink_p.txt 37
No such file or directory. 38
Symlinks are used more than hardinks and we can say that many services like the Web server Appache2 39
use symlinks extensively in their configuration. 40
Other properties of symlinks are: you can create a symlink to a directory. 41
ln -s /etc/ etc_symlink 42
I've created the symlink to /etc in the current directory. 43
However, you cannot create a hardlink to a directory. 44
This will be an error. 45
A symlink can cross the filesystem. 46
However a hard link is locally to the filesystem or the partition 47
The symlink has its own inode structure. 48
See how the Inode number of the file and the symlink are different. 49
However, all hard links have the same inode structure and inode number. A symlink contains only the path to the original 50
file or directory, not its contents. 51
The permissions of symlinks don’t matter because the permissions of the target file will be checked. 52
You’ll see that a symlink has all permissions. 53
Take your time to exercise and understand how symlinks work and how to create them . 54
They are used heavily in real world applications.