A Beginner’s Guide to Symbolic Links in Linux

Symbolic links, also known as symlinks or soft links, are special files that point to another file or directory on your system. They are like shortcuts that allow you to access the target file or directory without having to specify the full path.

Symbolic links are useful for various purposes, such as:

  • Organizing and sharing files across different locations
  • Creating aliases for long and complex directory paths
  • Linking libraries and executables for software development
  • Avoiding duplication of files and saving disk space

How to create symbolic links with the ln command

The ln command is the standard tool for creating links in Linux. By default, it creates hard links, which are different from symbolic links. Hard links point to the same inode (the low-level identifier of a file or directory) as the target file, while symbolic links point to the path of the target file (see section far below) .This means that hard links are more robust, but they cannot cross file systems or link to directories.

Read: How to manage permissions in Linux, guide for beginners

Linux command to create soft link

To create a symbolic link, you need to use the -s option with the ln command in Linux. The general syntax is:

ln -s target link

Where target is the file or directory you want to link to, and link is the name and location of the symbolic link.

Linux symbolic link directory example

For example, if you want to create a symbolic link named myownlink in your home directory that points to /var/log/syslog, you can run:

ln -s /var/log/syslog ~/myownlink

You can verify that the link was created successfully by using the ls -l command, which shows the details of files and directories. You should see something like this for the Linux symlink directory created above:

Read: 4 Ways to Find Large Files on Linux and Free Up Disk Space

The first character l indicates that it is a symbolic link, and the arrow shows the target file.

How to follow and delete symbolic links

To access the content of the target file or directory through a symbolic link, you can simply use the name of the link as if it were the original file. For example, if you want to view the last 10 lines of /var/log/syslog using the tail command, you can use either of these commands:

tail /var/log/syslog

tail ~/myownlink

They will produce the same output.

To find out which file or directory a symbolic link points to, you can use the realpath command, which shows the absolute path of a file. For example:

realpath ~/myownlink

This will output:

To delete a symbolic link, you can use the rm command, just like you would for a regular file. For example:

rm ~/myownlink

This will remove the link, but not the target file or directory.

Read: How to copy a file to multiple directories in Linux

Understanding Hard Links and Symbolic Links in Linux

What is a Hard Link?

A hard link is a direct reference to the data on the disk. It’s like having multiple names for a single file. When you create a hard link, you’re creating a new directory entry for a file. The file’s data exists as long as there’s at least one hard link referring to it.

Creating a Hard Link

To create a hard link, you can use the ln command followed by the source file and the link name:

ln source_file link_name

Differences Between Symbolic link and Hard link in Linux

While both types of links are used for similar purposes, there are key differences:

  1. Referencing: Hard links reference inode numbers (the unique identifier of each file or directory), while symbolic links reference file paths.
  2. File Systems: Hard links can only be created within the same file system, while symbolic links can span across different file systems.
  3. Linking to Directories: By default, you cannot create a hard link to a directory, but you can create a symbolic link to a directory.
  4. Deletion: If you delete the original file, the hard link will still access the content, but the symbolic link will be broken.

What is the Linux equivalent to symbolic links on Windows

In Windows, you can create symbolic links using the mklink command in the Command Prompt or the New-Item command in PowerShell.

Read: An introduction to Windows PowerShell for beginners

Conclusion

Symbolic links are a powerful feature of Linux that allows you to access files and directories from different locations without having to copy them or type long paths. You can create them using the ln command. You can also follow and delete them easily. Understanding how to use hard links and symbolic links in Linux can be incredibly useful for managing files and directories

 


If you like the content, we would appreciate your support by buying us a coffee. Thank you so much for your visit and support.

 

Nikolaus Oosterhof

Nikolaus holds a degree in software development and has a strong passion for all things tech-related, especially gadgets with screens. Though he is nostalgic for older phone models, he's a retired gamer and continues to enjoy programming in open-source environments. Additionally, Nikolaus enjoys writing about Linux, macOS and Windows and has experience designing web pages.

Leave a Reply