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
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.
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.
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:
The first character l indicates that it is a symbolic link, and the arrow shows the target file.
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:
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:
This will output:
To delete a symbolic link, you can use the rm command, just like you would for a regular file. For example:
This will remove the link, but not the target file or directory.
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.
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
While both types of links are used for similar purposes, there are key differences:
- Referencing: Hard links reference inode numbers (the unique identifier of each file or directory), while symbolic links reference file paths.
- File Systems: Hard links can only be created within the same file system, while symbolic links can span across different file systems.
- Linking to Directories: By default, you cannot create a hard link to a directory, but you can create a symbolic link to a directory.
- Deletion: If you delete the original file, the hard link will still access the content, but the symbolic link will be broken.
In Windows, you can create symbolic links using the mklink command in the Command Prompt or the New-Item command in PowerShell.
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.