In order to understand the differences between a hard link and soft link in linux or in general Unix-like operating systems, we first have to understand the concept of Inodes and what a File exactly is.


First a file in Unix-like operating systems is basically a link to an Inode.

An Inode however is a data structure which includes metadata about the files payload (content). So an Inode is not the actual content or the original name of the file. The metadata will include data like the creation date, file permissions, owner and more.

But most of all, the Inode will contain a reference where the actual payload (content) of the file is stored on the disk and will point to it.


As in Unix-like operating systems Everything is a file, an Inode is also a data structure with metadata and reference where it is stored on the disk for a directory, as a directory is also file.

Note that creating a hard link for a directory is not allowed! You can use a soft link for directory linking.

Unix file types
The seven standard Unix file types are regular files, directory, symbolic link, FIFO special, block special, character special, and socket as defined by POSIX.
Source: https://en.wikipedia.org/wiki/Unix_file_types


A file’s (link) inode number can be determined by using the ls -i command. To show this I will first create a simple text file quickly by using the cat command.

First a little excursus about how to quickly create a textfile in linux. You can use here the cat command as follows.

# cat > file1.txt

This command will create the file itself, when you pressing Enter, you won’t get back to the terminal prompt and instead the cursor is placed on the next line and wait for input. You can enter here simply your text and for new lines press enter.

To finish the file and getting back to the prompt, press CTRL+D.

By using the ls -i command you will see that the inode number for my new file file1.txt is 921412.





Hard Link

As we now know what an Inode and file is, it is simple to understand what a hard link is. A hard link is a file that points to an Inode.

Again!
An Inode is a data structure which includes metadata about the files payload (content) itself, so an Inode is not the content or its original name itself. That metadata will include data like creation date, file permissions, owner and more. Most of all the Inode will contain the reference where the actual payload (content) of the file is stored on the disk and will point to it.


Each file (content/payload) has a minimum of one hard link, which is referencing and pointing to the content of the file. The link is created by default when you create the file. Without this hard link, the operating system wouldn’t be able to locate the file (content).

A directory in contrast always has a minimum of two hard links by which it will be referenced, the first (.) is pointing to the directory itself and the second (..) is pointing to its parent directory.

The number of existing hard links for a directory or file you will see by using the ls -l command, the second column in the output will show the number of hard links exist.

So each directory has a hard link count of 2+n where n is the number of subdirectories. So when I create a subfolder in folder1, the count of hard links will increase to 3 as shown below.


You can create a hard link for a file with the ln command as follows

# ln <source> <link>
# ln -v <source> <link>

-v flag for verbose mode.

# ln <source> /different/folder/link

Will create the link in a different folder as the source file. Note that the destination should be within the same filesystem in contrast to soft links.


To verify the creation of the hard link we can use the ls -li command as follows.

# ls -li
# ls -li <target path>


Below I will create a hard link for the previously above created file1.txt. The file1.txt itself is also nothing more than a hard link (first and only hard link at the moment) to the corresponding Inode.

Finally I will check the creation with the ls -li command.

# ln -v file1.txt link_to_file1.txt
'link_to_file1.txt' => 'file1.txt'
# ls -li


As you can see, the new created hard link link_to_file1.txt has the same Inode number as file1.txt. So both files are referencing and pointing to the same Inode and therefore to the same file content (payload) on the disk.

No matter how many hard links you create for a file, as all pointing and referencing to the same Inode, they all be equivalent to each other, not matter which was first. Changing the content (payload) of the file will have the same impact on all hard links and in order to completely delete the file, all existing hard links have to be deleted.


To remove a hard link, you can use the rm or unlink command as follows.

# rm <link>
# unlink <link>


As mentioned above you can determine the amount of hard links to a file by using the command ls -l, here the second column will show you how many hard links exists for the file.

If you want to find all these hard links to a file, you first have to determine the Inode number of the file by using the ls -li command.

If I want to find all hard links for the file1 below, I will first have to note the Inode number 921415.

Then I can use the following command with that Inode number to list all hard links including there exact path located in the filesystem.

# find <path> -inum <Inode number>
# find / -inum 921415

I will search the whole filesystem by using the root directory


So you can see that for the file1 exists three more hard links besides the first one at creation of the file itself.




Soft Link (Symlink, Symbolic Link)

Soft links will be used to bypass the restrictions from hard links, which cannot be used to reference and point to files on a different filesystem and also cannot be used to point to a directory as mentioned above.

A soft link is a simple and small file which includes the path to the destination file or directory. The path itself can be relative (../folder1/file1) or absolute (/opt/folder1/file1).

Really simple a soft link is something like a shortcut in Windows

You can determine soft links by using the ls -l command as showing below. Ubuntu will color soft links in cyan followed by the destination path. Further you can see in front of the permissions is listed a l which indicates that the file or directory is a symbolic link.

As mentioned you can create soft links for cross-filesystem linking and for linking directories.

The downside of soft links is, that they not referencing and pointing to the actual Inode of the file, and therefore also not to the actual content (payload) of the file, instead they only pointing to an existing file (hard link, you remember), if this file is deleted or moved to another path, the soft link will be broken.

Below you can also see that the soft link has a different Inode number and therefore not pointing to the original Inode as with hard links.


In Ubuntu broken soft links will be colored in red. Below you can see that I renamed the original folder1 into folder11, therefore the first created soft link is broken and colored red.

Then I created a new second soft link which is pointing to the renamed folder11.


You can create a soft link for a file also with the ln command as for hard links above but in addition we must set the -s flag as follows.

# ln -s <source> <link>
# ln -vs <source> <link>

-s flag for soft link
-v flag for verbose mode.

# ln -s <source> /same_filesystem/different/folder/link
# ln -s <source> /different_filesystem/folder/link

In contrast to hard links, soft links can also be created on a different filesystem as the source file.



To remove a soft link you can use the rm or unlink command as also for hard links.

# rm <link>
# unlink <link>


To find all soft links in your operating system, you can use the following command.

# find / -type l


To find all soft links for a specific file, you can use the following command.

# find -L <source path> -xtype l -samefile <path to the specific file>
# find -L /var/www/ -xtype l -samefile /opt/file1

-L –> Follow symbolic links
-xtype –> File is symbolic link
-samefile name –> File refers to the same inode as name. When -L is set it includes symbolic links.




Symbolic Links and Hard Links in Windows

To create symbolic and hard links in Microsoft Windows, you can use the mklink tool.

mklink
https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/mklink

Creates a directory or file symbolic or hard link.

Hard Links and Junctions (also called a soft link)
https://docs.microsoft.com/en-us/windows/win32/fileio/hard-links-and-junctions

There are three types of file links supported in the NTFS file system: hard links, junctions, and symbolic links. This topic is an overview of hard links and junctions. For information about symbolic links, see Creating Symbolic Links.

File and Directory Linking

https://docs.microsoft.com/en-us/windows/win32/fileio/file-and-directory-linking




Links

Inode
https://en.wikipedia.org/wiki/Inode

Unix file types
https://en.wikipedia.org/wiki/Unix_file_types

Everything is a file
https://en.wikipedia.org/wiki/Everything_is_a_file

POSIX
https://en.wikipedia.org/wiki/POSIX