Creating File Links

Objectives

Allowing a single file to appear existing in multiple directories / locations via hard links or soft links.

Prerequisites

  1. Linux Operating System (Preferably CentOS)

Steps

A hard link maintains the same set of permissions and access times of the original file.

To create a hard link, simply use the ln command.

In the example below, a hard link of testfile is created in /home/testfile2

ln testfile /home/testfile2

To check your new hard link, simply run the ls -la command.

ls -la

If you received an error upon running the ln command such as the following: –

This simply means that you are trying to establish a hard link between 2 different partitions.

Do note that hard links cannot be established between different file systems or partitions.

To resolve this, simply ensure the hard link is created within the same file system and partition. Alternatively, you may use soft links / symbolic links.

On the other hand, symbolic links / soft links provide greater flexibility compared to hard links as they can: –
1. Create links between different file systems and partitions.
2. Create links to nonexistent files.
3. Create links to directories or files.

To create a symbolic link, simply add the -s option in the ln command.

In the example below, a soft link is created to testfile3 (nonexistence & partition 1) with /home/testfile2 (exist & partition 2).

ln -s /home/testfile2 testfile3

Verify the link by running the ls -la command.

To remove a symbolic link, simply use the rm command.

In the following example, we will remove the symbolic link with testfile3.

rm testfile3

The above will trigger a warning message stating to confirm the deletion. Simply press y to proceed.

Check the directory again with ls -la, the symbolic link should be removed.

Updated on August 28, 2020

Was this article helpful?

Related Articles

Need Help?
Submit a ticket to us and let our professional team assists you

Support Billing Sales
Support
Billing
Sales