logo
linux

Hard and Soft file links in Linux

Hard and Soft file links in Linux
6 min read
#linux

In Linux, everything is a file. We use links to access deeply nested files in a file system easily. We learn about soft and hard links in Linux, including how to create and remove them.

main point here

Introduction.

A link is a connection between a filename and the actual data stored on the disk. A link can either be hard or soft. In this article, we learn about hard and soft links in Linux.

A symbolic link is a file or directory that points to another file or directory. Symbolic links/symlinks in Linux are what shortcuts are in Windows.

Soft links/symlinks/symbolic links in Linux are what shortcuts are in Windows. A soft link is just a pointer to a file or directory, it does not have the contents of what it is pointing to.

This type of link can link files or directories across different file systems. We refer to them as soft since when the files or directories they point to are moved, replaced, renamed, or deleted, they are broken/dangling and therefore need to be removed or relinked.

The size of a soft link will be the size of the name of the file it is pointing to, that is, if we have a file file.txt whose name is 5bytes in size, this will be the size of its symbolic link.

Hard links are shortcuts for files but cannot be created for files in other file systems. A hard link unlike a soft link will have the actual contents of the file or directory it is pointing to. When we create hard links, they point to the inode of the original file in the storage disk. We cannot create hard links for directories. This is a measure to avoid recursive loops. Also unlike soft links which are broken when the files they point to are moved, renamed, deleted, hard links are not affected when this happens hence the term hard. Note that if we create a hard link and change it changes its size, these changes are also reflected in the original file.

ln command syntax.

In Linux, we use the ln command to create links, both soft and hard links. We write ln commands using the following format:

$ ln [OPTION]... [-T] TARGET LINK_NAME
Image

To create a symbolic link, we use the following syntax:

$ ln -s [FILE TO BE LINKED] [PATH OF LINK]

Here we use -s to indicate that this is a soft link. We can also use --symbolic option.

If -s or --symbolic is not specified, a hard link is created by default. For example to create a soft link for a file file.txt we write:

$ ln -s file.txt fileLink.txt

We will have a new file with an arrow as shown below:

Image

This represents a soft link

Now let's create a hard link, for this, we execute the previous command without the -s option:

$ ln file.txt fileHardLink.txt

As mentioned earlier hard links will have the same contents as the files they point to. To verify this we can confirm the inode of the original file and the hard link by listing the files, this time using the -i option:

$ ll -i
Image

Notice that the inodes for the hard links and the original file are similar.

Note that after creating a symbolic link, we cannot create another link with a similar name. An error will be presented in such a situation, however, we can use the --force or -f option to force such an action. For example:

$ ln -sf file.txt fileLink.txt
Image

Here we create a new symbolic link and try to create another with a similar name. An error will occur when we try to create a similar one, to overwrite the previous we apply the -f option which stands for force.

Before this operation, we should make sure that the symlink is indeed a symbolic link. To do this we can execute the following command:

$ ls -l [symlink]

After this command, we should have the file attributes that we use to identify this file. Also, we have an arrow at the end of the file, this means that this file is a symbolic link.

We remove a symbolic link using the unlink command followed by the name of the symbolic link:

$ unlink [symlink]
Image

In case the symbolic link is of a directory, we don't add the / at the end since Linux assumes that it is indeed a directory instead of the intended symbolic link.

We can also remove a symbolic link using the rm command. Here we just delete it normally.

$ rm [symbolic link]

What's the difference between the two? Well, with the rm command we can list multiple symbolic links are remove them all at once.

A broken link is a link that is not pointing to anything, that is, it points to a renamed, deleted, or moved file or directory.

We find broken links using the following command:

$ find [directory] xtype l
Image

Above, we try to locate broken links in the directory. First, we delete the file the soft link is pointing to hence breaking it.

To delete the broken link, we pass the -delete option as follows:

$ find [directory] -xtype l -delete

After this operation, the soft link is deleted.

Image

Summary.

Some commonly used files and directories are deeply nested inside the Linux file system. We use symbolic links to make them easily accessible without moving them from their original directories, this maintains file consistency in the file system. We also use links to link libraries, storing copies of files in multiple locations in the file system for easier access.

References

references here