Home » Linux » Shell » Linux Symlinks

How to Create Linux Symlinks Using the ln Command

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.
        • The file system will keep count of how many references there are to the file on the disk.  When all hard links are deleted, that count will be 0 (zero).  When the count reaches zero, the file is deleted and the data space will be de-allocated so that it can be used to store other files.
      • 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.

Syntax

ln [OPTIONS] SOURCE_PATH LINK_PATH

Note that:

  • Both files and folders can be used as the SOURCE_PATH
  • The soft like will be created at LINK_PATH

Options

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:

man ls

Examples

To demonstrate creating a simple symbolic link (symlink for short), we can create a file:

touch test_file.txt

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:

ls -l

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

Note that:

  • 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

Conclusion

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.

New to Linux? Put it straight to use by learning some code!

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment