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

Julian Mendez
3 min readOct 18, 2020

Symbolic link

In computing, a symbolic link, on Unix or GNU/linux systems, indicates an access to a directory or file that is in a different location within the directory structure. A modification made using this link will be reflected in the original; however, if the link is removed, the authentic link will not be removed.

Another less usual option is to use a hard link, in which the access is indistinguishable from the real one, and the deletion of the link causes the deletion of the file or directory if it was the last hard link to the file.

Creating Symlink To a File

To create a symbolic link to a given file, open your terminal and type:

ln -s source_file symbolic_link

Replace source_file with the name of the existing file for which you want to create the symbolic link and symbolic_link with the name of the symbolic link.

The symbolic_link parameter is optional. If you do not specify the symbolic link, the ln command will create a new link in your current directory:

In the following example, we are creating a symbolic link named my_link.txt to a file named my_file.txt:

ln -s my_file.txt my_link.txt

To verify that the symlink was successfully created, use the ls command:

ls -l my_link.txt

The output will look something like this:

lrwxrwxrwx 1 linuxize users  4 Nov  2 23:03  my_link.txt -> my_file.txt

The l character is a file type flag that represents a symbolic link. The -> symbol shows the file the symlink points to

Hard link

In computing, a hard link is a reference or pointer to a file (the physical data) on a file system.

Hard links associate two or more files sharing the same inode. This makes each hard link an exact copy of the rest of associated files, both data and permissions, proprietary.

How to Create Hard Links in Linux

To create a hard links in Linux, we will use ln command. For example, the following command creates a hard link named tp to the file topprocs.sh.

$ ls -l
$ ln topprocs.sh tp
$ ls -l

Looking at the output above, using ls command, the new file is not indicated as a link, it is shown as a regular file. This implies that tp is just another regular executable file that points to the same underlying inode as topprocs.sh.

To make a hard link directly into a soft link, use the -P flag like this.

$ ln -P topprocs.sh tp

Differences between soft and hard links

Symbolic links can be made with files and directories while hard links can only be made between files.

Symbolic links can be made between different file systems, hard ones cannot.
Hard links share the inode number, symbolic links do not.

On symbolic links if the original file or directory is deleted, the information is lost, on hard links it is not.

Hard links are exact copies of the file while symbolic links are mere pointers or “shortcuts”.

sources:

https://rm-rf.es/diferencias-entre-soft-symbolic-y-hard-links/

https://www.tecmint.com/create-hard-and-symbolic-links-in-linux/

https://es.wikipedia.org/wiki/Enlace_duro

https://linuxize.com/post/how-to-create-symbolic-links-in-linux-using-the-ln-command/

--

--