Hard Links Versus Symbolic Links

A laptop displays a screen full of code.
Photo by Clément H on Unsplash

When linking files in a Linux system, it’s important to think about your goal. Are you trying to save an exact copy of the file? Are you trying to make it easier to reach in a different directory?

Depending on these goals, you will want to use either a hard link or a soft link. While both will direct you to a file, they serve different functions and may lead you to different versions of a file.

Hard links

A hard link is an exact link to a file’s inode. This means that even if you change the content of the file, the hard link will still direct to the original file contents.

An inode stores a file’s data, including metadata, file permissions, and owner.

To create a hard link

ln [file] [link]

For example, to create a link to thisfile using thislink, you would use:

ln thisfile thislink

Soft links

A soft link is like a shortcut to a file. Rather than linking directly to the inode of a file it links to the file’s location in a specific directory.

Be careful — since a soft link references a specific location, it will not work if you move the original file to a different location. However, if you create a new file of the same name in the same location, a soft link will direct you to the new file.

To create a soft link

ln -s [source] [link]

For example, to create a soft link to thisfile using thislink, you would use:

ln -s thisfile thislink

So what’s the difference?

Hard links will send you straight to an exact version of a file while soft links will direct you to the current version of the file.

Why is this important?

Let’s bring it back to your motivation for creating a link. If you want to preserve an exact version of a file, you’ll want to use a hard link. If you want to make it easier to reach a file, you’ll want to use a soft link.

Knowing the difference is also critical if you plan on moving the location of files or preserving the metadata.

Software engineering student and lover of mountains.