What is the difference between a hard link and a symbolic link?

Yellow Pages

The File System of UNIX

In the UNIX Operating System, and UNIX-like systems, everything is either a file or a process. In this article, we will look closely at files on UNIX systems to better understand symbolic and hard links.

There are three kinds of files in the UNIX system — ordinary files, directory files, and special files.

Ordinary files, or files in a UNIX Operating System are a collection of related data that are under the same name. To the user, files appear as one continuous block. Directories, or folders, are files that lists other files.

Directory files, or directories, is a file that lists other files.

Special files represent input and output devices.

Files in UNIX are organized in a hierarchical structure that resembles a tree-like structure.

Credits to The Linux Documentation Project (TLDP)

Like a tree, there is a root where everything starts from. The root of your tree-like file system will begin at the root directory. The root directory is represented by a forward slash ( / ).

If we delve deeper into a file system, we’ll see that each file has an associated data structure called an inode.

What is an inode?

An inode is a data structure that includes the following information:

  • Length of the file.
  • Most recently accessed or modified time of file.
  • Time of the most recent modification.
  • Owner and group information (access permissions)
  • Number of links to the file.
  • Location of where the data in the file is stored on your disk.

The most important feature of inodes is that it records the actual location of the data in the file. We can think of an inode as an entry in the Yellow Pages! An entry in the Yellow Pages, like an inode, stores information like addresses. To refer to an inode, we use the filename.

A computer’s hard drive can be partitioned into separate parts. Each partition will have its own file system. Subsequently, each partition will have its own set of unique, inode values.

Now that we have an overall understanding of the file system in UNIX systems, we can begin exploring hard links and symbolic links.

Hard Links and Symbolic Links

In general, a link is a way of associating multiple file names to the same file data. We can form this link by making a hard link or a soft link.

Hard Link

A hard link is formed when two or more file names are tied to the same inode. Since each partition in the hard disk has their own set of unique, inode numbers, hard links cannot span partitions.

Symbolic Link

A symbolic link is a small file that acts as a pointer to a file. In this file, the path of the target file, and not the physical address on the hard disk, is recorded. Since a symbolic link depends on the path to the target file, and not the actual address of the inode, symbolic links can span partitions.

Now that we know what a link is, how can we create one? Let’s take a look at the next section.

How Do We Create a Hard Link or Symbolic Link?

To create a link between files, we use the following command: ln. We can create a hard link or a symbolic link with this command.

General format:

Manual page for ln

If we use the first line under synopsis to format our command, we can create a link to the source file with the name of target file.

In the second line, we can create a link to the source file in the target directory.

Symbolic Link

To create a symbolic link, we can use the following command:

Creating a soft link

Hard Link

To create a hard link, we can use the following command:

Creating a hard link

Difference Between a Hard Link and a Symbolic Link

A symbolic link is a pointer to the target file.

A hard link is a pointer to the inode of the target file.

As a result, the following behaviors ensue:

  • Hard links possess the equal size of the original file.
  • Symbolic links are small files.

Now that you’ve learned about symbolic and hard links, go forth and make your own!

Sources:

https://www.cs.dartmouth.edu/~campbell/cs50/shell.html

http://www.geekride.com/hard-link-vs-soft-link/

Like what you read? Give Susan Su a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.