This tutorial explains how to create symlinks (symbolic links), also know as “Soft Links,” in Linux using the ln command.
If you’re coming to Linux from a background using the Windows operating system, you’ll be familiar with the concept of shortcuts – files that don’t contain any real data and contain a link to the actual file or folder you wish to access.
They may exist simply for your convenience (to save time clicking through nested folders) or to redirect the output (You have a program with a hard-coded directory to which it outputs files, and you wish to send them somewhere else).
Soft Links (Symlinks) in Linux serve essentially the same purpose.
Hard Links vs. Soft Links
- Soft Links (also known as Symbolic Links or Symlinks) refer to the location of another file or folder on a filesystem.
- Therefore, if the file that has been soft linked to is deleted, the soft link is meaningless as it points to a file that doesn’t exist.
- As the soft link is just a reference to the linked file location, they can exist on different filesystems.
- Permissions are not shared between link and linked.
- Hard links are a mirror copy of the file linked – both copies refer to the same data on a storage device.
- Therefore, deleting the hard-linked file won’t remove the data – the other “Hard Link” to the data will still exist as a file in the filesystem.
- As they are two references to the same data, they must exist on the same filesystem.
- Permissions are shared – both linked and linked are the same file on disk being referenced from two places.
ln [OPTIONS] SOURCE_PATH LINK_PATH
- Both files and folders can be used as the SOURCE_PATH
- The soft like will be created at LINK_PATH
You can pass the following options to the ln command:
|-F||If the proposed link (link_name) already exists and is a directory, then remove it so that the link may occur. The -F option should be used with either -f or -i options. If none is specified, -f is implied. The -F option is a no-op unless -s option is specified.|
|-h||If the link_name or link_dirname is a symbolic link, do not follow it. This is most useful with the -f option, to replace a symlink which may point to a directory.|
|-f||If the proposed link (link_name) already exists, then unlink it so that the link may occur. (The -f option overrides any previous -i options.)|
|-i||Cause ln to write a prompt to standard error if the proposed link exists. If the response from the standard input begins with the character y or Y, then unlink the proposed link so that the link may occur. Otherwise, do not attempt the link. (The -i option overrides any previous -f options.)|
|-n||Same as -h, for compatibility with other ln implementations.|
|-s||Create a symbolic link.|
|-v||Cause ln to be verbose, showing files as they are processed.|
These options and full details for the ls command can be found in the user manual by running:
To demonstrate creating a simple symbolic link (symlink for short), we can create a file:
Then we can create a soft link to it:
ln -s test_file.txt link_to_test_file
You can verify the link by running:
Which will output something like:
-rw-r--r-- 1 user staff 0 2 Jan 01:31 test_file.txt lrwxr-xr-x 1 user staff 9 2 Jan 01:32 link_to_test_file -> test_file.txt
- The letters in the first column represent the file or folder type and permissions
- The number following is the number of files contained
- user/staff are the user/group with permissions to the file or folder
- Followed by the file size, modification date, and name
- See that l at the beginning of the row? This means that it is a link
- Additionally, you can see the -> bit at the end, which is showing that the file is linked to test_file.txt
- So, it’s definitely a link
When I log into most systems, I’m sent straight to the user’s home directory. Rather than typing out the path to the directory, I want to be working in that day (for example, in a directory hosting the files for a web app I’m deploying), the first thing I’ll usually do is create a soft link to that folder so that I don’t have to type out the full path every time I want to navigate back to it.
So, it’s pretty useful.