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

ln is the command in bash that makes links. When you create a link, it can either be a hard, or symbolic link. These have different meanings and where they point to in the computer’s memory.

Let’s look at the diagram above. inode refers to the place in memory in which the file is stored. Both my-hard-link and myfile.txt are considered hard links as they point directly inode.

my-soft-link is a symbolic link as it points to a link that references inode. If you were to move myfile.txt, you would remove the reference of my-soft-link and it would no longer be able to find the same place in memory. However, my-hard-link would still have the reference in memory to point to.

The format of the ln command is ln [-"flag"]source_file [target_file]. To create a hard link you would only need the command ln source_file [target_file , no flags are needed. To create a symbolic link ln -s source_file [target_file . Now that we have a sense of hard vs symbolic links, lets follow the example below.

I have used our diagram above to create an example of how hard links and soft links work. As you can see with my-soft-link it points directly at myfile.txt showing that it is linked.

The file inode contains the string “apples!!!” and all of the files are linked just in the same way as the diagram in the first picture.

now let’s rename the inode and see what happens to my-hard-link.

my-hard-link produces the string “apples!!!” because it is pointing the same place in memory as inode.

Now let’s change the name of what my-soft-link is pointing at.

As you can see here, the name of myfile.txt got changed to differentFile, my-soft-link no longer contains the string “apple!!!” because a soft link points to a name rather than a place in memory.