Linux Symbolic/Hard Links

To understand links in a file system, you first have to understand what an inode is.

In linux there are two types of links :
Soft/Symbolic Links
Hard Links

Hard Links

Every file on the Linux filesystem starts with a single hard link.
The link is between the filename and the actual data stored on the filesystem (directory entry > inode > data blocks).

When you create a hard link you create a file that gets the same inode as the target file.
You have different file names/paths for a unique physical file on a partition (pointing to the same inode)

Hard links can only be created for regular files (not directories or special files) and ONLY within the same filesystem.
A hard link cannot span multiple filesystems
.

If you delete the "original file", you can still access it via any remaining hard link having the same inode.
Apart from the filename/filepath, you cannot tell which one is the hard link since they share the same inode.

$ ln /path/to/target_file /path/to/hardlink
List inodes files from <dir>, Recursive, No Sort, date (mtime)
CMD_LS_RECURSIVE_INODE_MTIME_NOSORT
OUTPUT:
<inode> <mode?rwx> <links> <uname> <gname> <size_bytes> <date YYYY-MM-DD> <time hh:mm:ss> <filepath>

$ LC_ALL=C ls -ilR --time-style='+%F %T' <dir> 2>/dev/null | sed -e '/:$/,/^total [0-9]\{1,\}/d' -n -e '/^[0-9]\{1,\} -/p' | tr -s '\n'


List inodes with hard link(s) from <dir>, Recursive, Natural Sort (inode first), date (mtime)
OUTPUT:
<inode> <mode?rwx> <links> <uname> <gname> <size_bytes> <date YYYY-MM-DD> <time hh:mm:ss> <filepath>

$ <CMD_LS_RECURSIVE_INODE_MTIME_NOSORT> | awk '$3 > 1 {print $0}' | sort


Find inodes with hard link(s)
OUTPUT: <inode>
$ <CMD_LS_RECURSIVE_INODE_MTIME_NOSORT> | awk '$3 > 1 {print $1}' | sort -u

Soft/Symbolic Links

When you create a soft link, you create a new file with a new inode, which points to the target path.
It doesn’t reference the target inode. If the target’s path/name changes or is deleted, the reference breaks (pointing to a nonexistent file path).

Symbolic links can link together non-regular and regular files.
They also can span multiple filesystems.

A symbolic link is identified by the mode lrwxrwxrwx, you cannot change it, so it is easy to identify them.
The symbolic link’ size is the target’s path length.

Changing owner, group, permissions of a symbolic link only has effect on the target file, in this case target file’s ctime is updated.

A change to a symlink name, updates its access time (atime) and status time (ctime).
That is the only thing you can change for the symbolic file itself.

$ ln -s /path/to/target_file /path/to/symlink