Home Linux The Magic of Symbolic Links: Understanding The Basics

The Magic of Symbolic Links: Understanding The Basics

Symbolic Links are quite useful because they allow us to store files wherever we want, regardless of a program’s need to store them in a particular place.

I’ll be giving an overview of everything about symbolic links. The links are quite easy to create too. There are a few ways to do that, and I’ll also explain those ways in easy-to-follow steps below.

What Will I Learn?💁 show

Symbolic Links: What Are They? 🤔

These links represent actual files or folders. Normally, shortcuts take you to where files, apps, or folders are located. But these links stand in as the files and folders themselves. 

When you create a symbolic link to a folder, there is no difference between clicking that folder or clicking the link.

This feature is why most folks call them “advanced shortcuts”. These links can be useful in many situations such as the one below.

Example

Let’s say you have a warfare app that needs its files stored in a particular directory (say C:\Warfare). But you want to declutter that particular directory and there’s nothing else you can afford to remove except the files for the app. 

What you’ll do is move the app’s directory to another directory (say D:\GamesFolder), then all you’ll have to do is create a symbolic link at C:\Warfare that will point to D:\GamesFolder

What now happens is that whenever you try to launch your app, it’ll naturally go to C:\Warfare and then get directed back to D:\GamesFolder for the files by the symbolic link you created.

All of these processes will be done without any delay and the app will still believe that its files are stored in its preferred location. Sounds cool, right?

Other Reasons For Using Symbolic Links

You can use the links for a variety of reasons. It also helps when working with programs like Google Drive. 

You can sync folders Google Drive using the links even if the files and folders aren’t located in their own folder (something most apps insist upon before syncing). 

You don’t have to worry about symbolic links taking up double the memory space too. The links are virtual folders (contain nothing more than code that maintains an appearance of the folder or files you linked).

It is also important to know that not only Windows 10 supports symlinks. Windows Vista, 7, 8 versions also support symbolic links. 

The Two Types Of Symbolic Links

The generally known names for the two types of links that exist are hard links and soft links. But I need to point out that Windows refers to soft links as symbolic links. 

So, in any Windows document, you’d see the two types of links as hard links and symbolic links.

To ensure better understanding, I will stick to the hard and soft links naming as I explain their uses and differences.

Soft (Symbolic) Links 

Soft links redirect queries to where the actual files or folders exist. 

So if you click on a soft link to get the files of a program, you’ll be taken to where the files of that program exist. 

Hard Links

Hard links are quite different and more intense. These links don’t redirect queries. 

When you click on them, the links act as if the files and folders you’re looking for are exactly in the link (hard) you clicked on. 

This is what makes apps and programs not know any better that their files or folders have been moved from preferred storage locations to other directories. 

Creating Symbolic Links In Windows

Learning how to create symbolic links just needs a little basic knowledge about systems and using command prompts. 

You can create symbolic links using the two ways explained below. 

The first one is internally sourced while the second option involves downloading a graphical tool that would make the job a whole lot faster and probably easier especially for folks who aren’t tech-savvy.

If you’re not comfortable with downloading 3rd party tools, then you should just stick to the first option.

Creating Symbolic Links Using The mklink Command

The first step is opening a command prompt with administrator privileges. 

There’s a Windows 10 update (Creator’s Update) that allows you to use the command prompt (without admin privileges) for something like this but you’ll have to enable developer mode first. 

To enable the mode, follow this navigation.

Settings > Update & Security > For Developers

The Magic of Symbolic Links: Understanding The Basics

If you want to avoid all the steps above, just go to your Start menu and look for the Command Prompt shortcut, right-click the shortcut and then choose Run As Administrator in the pop-up menu that appears to launch the prompt window.

Note: If you don’t run the command prompt with admin privileges when you get to enter the command, you’ll get a message saying you don’t have sufficient privilege to perform the operation.

Creating a Soft Link Pointing To a File

This command creates a soft link at Link that points to the file at Target

mklink Link Target

How To Create Symbolic Links

Creating a Soft Link Pointing To a Directory

This command creates a soft link too but one that points to a directory at Target.

mklink /D Link Target

Creating a Hard Link Pointing To a File

This hard link command creates a link at Link that points to a file at Target.

mklink /H Link Target

Creating A Directory Junction

A directory junction just basically means creating a hard link pointing to a directory. 

mklink /J Link Target

These are the commands you need to create symbolic links using the mklink command. 

Example

I’ll give an example of how a command will look below. I’m going to try and create a directory junction. 

mklink /J C:\Warfare C:\GamesFolder

Please note that when you have paths that have spaces, you will need to tweak your command by adding apostrophes. Here’s an example below.

mklink /J “C:\War fare” “C:\Games Folder” 

The Magic of Symbolic Links: Understanding The Basics

Creating Symbolic Links With This 3rd Party Graphical Tool (Link Shell Extension)

The first step is downloading a package needed for the tool to run effectively. You should download the package first even before the tool

Both of these files (package + tool) can be found on the download page.

When you’re done with downloading and installing the tool. Launch it. 

You’ll see the option to access your files and folders. Do that and navigate to the file or folder you want to create a link to.

When you find the file or folder, hover on it and right-click. In the pop-up menu, select the option that reads Pick Link Source.

The Magic of Symbolic Links: Understanding The Basics

Now go to the file or folder you want to create the link to. When inside, use the Drop As option to create a link to the file or folder you earlier selected in this new file or folder you chose.

The Magic of Symbolic Links: Understanding The Basics

There are three options for you to choose from when you click on Drop As.

  • Hardlink – Creating a hard link to a file.
  • Junction – Creating a hard link to a directory.
  • Symbolic Link – Creating a soft link to both files and directories.

Using a third-party graphical tool to create links is quite straightforward. The important thing is to know the functions of the menu (as outlined above).

There are always security concerns whenever you want to download third-party apps and you should know that you are open to a level of risk (minimal in this case). 

You will get a warning that this third-party tool download can’t be done securely and your Windows Defender might even tell you it’s unsigned. 

If you choose to, still go ahead and download it.

Deleting Symbolic Links In Windows

This is quite easy to do. You can delete it by right-clicking on the link and selecting the Delete option in the pop-up menu that appears. 

Just like you would delete any file or folder, you could use shortcut keys too. Select a link and click Delete.

But I have to warn you about the danger of deleting the actual files or folders instead of the link (especially in cases of hard links). 

These links fool applications. If you’re not careful they might trick you into deleting files or folders too. 

You might want to double-check which one’s the Link and which one’s the Target first before deleting anything. 

Creating Symbolic Links In Linux (For Files)

Creating links in Linux relies on ln and -s (used in choosing which symbolic link). The format below is quite easy to follow.

ln -s [target file] [symbolic filename]

In – This command creates links between source files and directories

-s – This is the command for specifying the type of command you want.

Target File – This is the file you’re creating a link for. 

Symbolic Filename – Named symbolic because it will act as the target file itself but is just a link.

Verifying created links by directory listing is done by using the detailed list command.

ls -l

In cases where you don’t input any symbolic filename, the link will still be created. But it will be created in the same directory as the target file.

Creating Symbolic Links In Linux (For Folders)

The idea here too is similar to creating symlinks in the files above. Here’s the basic format to follow.

ln -s [specific file or directory] [Symlinks name]

All you have to do is input the directory you want to create a link to and input the name of the link you want to create, then run the command.

Example

ln -s /user/local/download/fm /games

In this command line above, I’m trying to link the /user/local/download/fm directory to the /games folder.

Once the link is created in the /games folder, any attempt by programs to access the directory in the folder will work as if the directory is in the folder.

The More Common Symbolic Link Options (Linux)

Below is a list of some of the useful symbolic link options that users might want to try out. 

Please note that Symbolic Links are also called command-line switches.

  • -backup[=CONTROL] – this command backs up the existing destination files.
  • -f, –force – this command removes existing destination files.
  • -L, –logical – this command dereference symbolic link targets.
  • -d, -F, –directory – gives super users the privilege of attempting hard links.
  • -l, –interactive – this is a prompt before destination files are removed.
  • -r, –relative – this helps in creating a link that is relative to the link location.
  • -n, –non-dereference – symbolic links to the directory are taken as files.
  • -s, –symbol – This ensures symbolic, not hard, links are created. 
  • -P, –physical – making hard links to symbolic links directly.

Removing Symbolic Links In Linux

There are two ways you could change or remove symbolic links in Linux. Both ways are easy to follow. 

  • By the unlink command
  • By the rm command

Using The Unlink Command

This way of removing symbolic links in Linux is straightforward. 

All you have to do is follow the format (command) below whenever you’re trying to remove the links.

unlink [symbolic link to remove]

To follow this format with an actual link, all you have to do is input the symbolic link’s name (the one you used in creating the link).

Using The rm Command

This is also similar to the command above, they basically both do the same thing. Here’s the format below.

rm [symbolic link to remove]

What is a symbolic link example?

Example 1:-

Here is an example of how to create a symbolic link in a Linux/Unix-based operating system:-

Let’s say you have a file located at /home/user/documents/report.txt, but you want to access it from another directory, say /home/user/downloads/. You can create a symbolic link to the file using the ln command with the -s flag, like this:-

ln -s /home/user/documents/report.txt /home/user/downloads/report_link.txt

This creates a symbolic link named report_link.txt in the downloads directory that points to the original file in the documents directory. You can now access and manipulate the file using either the original path or the symbolic link.

Example 2:-

Here’s an example of how to create a symbolic link using the ln command in a Linux terminal:-

Suppose you have a file called file1.txt located in /home/user/documents/.

To create a symbolic link for file1.txt in your home directory, you would use the following command:

ln -s /home/user/documents/file1.txt ~/file1_link

This creates a new file called file1_link in your home directory that points to the original file1.txt file. The -s option tells the ln command to create a symbolic link instead of a hard link.

You can now access the contents of file1.txt by opening file1_link. Any changes made to file1.txt will also be reflected in file1_link, and vice versa.

Example 3:-

Here’s an example of how to create a symbolic link in Unix-based systems, such as Linux or macOS:

Suppose you have a file called “mydocument.txt” in the directory “/home/user/documents/”, and you want to create a symbolic link to this file in the directory “/home/user/downloads/”. You can do this using the following command in the terminal:

ln -s /home/user/documents/mydocument.txt /home/user/downloads/mydocument_link

Now you can access the file “mydocument.txt” from both directories “/home/user/documents/” and “/home/user/downloads/” by using either the original file or the symbolic link. For example, you can open the file “mydocument.txt” by typing either of the following commands in the terminal:

nano /home/user/documents/mydocument.txt
nano /home/user/downloads/mydocument_link

Both commands will open the same file, because the symbolic link points to the original file.

How to Remove symbolic link?

To remove a symbolic link in Linux, you can use the “rm” command followed by the name of the symbolic link.

Here is the syntax:-

rm [symbolic link name]

For example, to remove the symbolic link named “mylink”, use the following command:-

rm mylink

This will remove the symbolic link from the file system. Note that removing a symbolic link does not affect the target file or directory that it points to. If you want to remove the target file or directory as well, you will need to remove it separately.

📗FAQ

What are symbolic links to files?

Symbolic links, also known as soft links, are files that act as pointers to other files or directories. When a symbolic link is created, it contains the path to the target file or directory.

When the symbolic link is accessed or opened, it redirects the user to the target file or directory. Symbolic links can be created in Unix-based operating systems, as well as in Windows.

What is hard link vs symbolic link?

A hard link is a reference to a file or directory that is created in the file system. Unlike symbolic links, hard links do not contain a path to the target file or directory; they simply point to the same inode, which is the data structure used by the file system to represent a file or directory.

Hard links can only be created within the same file system, whereas symbolic links can point to targets on any file system.

What is the purpose of a symbolic link?

The purpose of a symbolic link is to allow users to create references to files or directories that are located in other parts of the file system.

This can be useful for organizing files or for creating shortcuts to frequently used files or directories. Symbolic links can also be used to allow multiple users or applications to access the same file or directory.

Symbolic link Linux command

The symbolic link Linux command is “ln -s”. The “ln” command is used to create links between files or directories, and the “-s” option specifies that a symbolic link should be created rather than a hard link.

To create a symbolic link using the “ln” command, the syntax is as follows:-

ln -s [target file or directory] [symbolic link name]

For example, to create a symbolic link named “mylink” that points to a file named “myfile.txt” in the current directory, use the following command:

ln -s myfile.txt mylink

This will create a symbolic link named “mylink” that points to “myfile.txt”. When the symbolic link is accessed or opened, it will redirect the user to “myfile.txt”.

What is a symbolic link and why is it useful?

A symbolic link is a file that acts as a pointer to another file or directory. It is useful because it allows users to create references to files or directories that are located in other parts of the file system.

This can make it easier to organize files, create shortcuts to frequently-used files, and allow multiple users or applications to access the same file or directory.

What is the alternative to symbolic link?

The alternative to symbolic links is to copy the file or directory to the desired location. However, copying a file or directory creates a new instance of the file or directory, which can take up additional space on the file system.

Hard links are another alternative to symbolic links, but they can only be created within the same file system.

How do I create a symbolic link in command prompt?

To create a symbolic link in the command prompt, use the “mklink” command followed by the name of the symbolic link and the path to the target file or directory.

For example, to create a symbolic link named “mylink” that points to a file named “myfile.txt” in the current directory, use the following command: “mklink mylink myfile.txt”.

What is the difference between symbolic link and copy?

The main difference between a symbolic link and a copy is that a symbolic link is a reference to the original file or directory, whereas a copy is a new instance of the file or directory.

This means that symbolic links take up less space on the file system than copies, but changes made to the original file or directory will also affect the symbolic link.

Is a symbolic link a file or a directory?

A symbolic link is a file that contains a path to another file or directory. It is not a directory itself, but it can point to a directory.

What is the difference between a symbolic link and a shortcut?

In Windows, a symbolic link and a shortcut are similar in that they both provide a way to access a file or directory from a different location.

However, symbolic links are created using the “mklink” command and are a feature of the file system, while shortcuts are created using the “Create Shortcut” command in the Windows GUI.

What are the drawbacks of using a symbolic link instead of hard link?

One drawback of using a symbolic link instead of a hard link is that changes made to the original file or directory will also affect the symbolic link.

Additionally, symbolic links can only be used to reference files or directories that are located on the same file system or on a different file system that is mounted within the current file system, whereas hard links can only be created within the same file system.

Is a symbolic link a type of file?

Yes, a symbolic link is a type of file that contains a path to another file or directory.

What is one advantage of symbolic links over hard links?

One advantage of symbolic links over hard links is that symbolic links can be used to reference files or directories that are located on different file systems or partitions.

Can a symbolic link be shared?

Yes, a symbolic link can be shared by multiple users or applications.

Can I create a symbolic link to a directory?

Yes, a symbolic link can be created to a directory using the same “mklink” command used to create a symbolic link to a file.

What is the difference between symbolic link and Windows?

Symbolic links are a feature of the file system and can be created in Unix-based operating systems as well as in Windows. In Windows, symbolic links are created using the “mklink” command in the command prompt.

What is one difference between an alias and a symbolic link?

An alias is a reference to a file or directory that is created within an application or the operating system, whereas a symbolic link is a file that contains a path to another file or directory in the file system.

Can you create a symbolic link in Windows?

Yes, symbolic links can be created in Windows using the “mklink” command in the command prompt.

What is the difference between junction and symlink?

Junctions are a type of symbolic link that are specific to Windows and are used to create links between directories. Symbolic links, on the other hand, can be used to create links between files or directories.

How do I enable symbolic links in Windows?

To enable symbolic links in Windows, the “Local Security Policy” or the “Group Policy” settings must be modified to allow symbolic links. This can be done through the Windows Control Panel or using the “gpedit” command in the command prompt.

How do I copy a directory with symbolic links?

To copy a directory with symbolic links intact, use the “robocopy” command in the command prompt with the “/sl” option. This will copy the symbolic links as symbolic links rather than creating new copies of the files or directories.

How do I find all symbolic links in a directory?

To find all symbolic links in a directory, use the “dir” command in the command prompt with the “/al” option. This will display a list of all files and directories in the directory that are symbolic links.

Is a symbolic link a pointer?

Yes, a symbolic link is a file that acts as a pointer to another file or directory.

What happens to symbolic link when file is deleted?

If the target file or directory of a symbolic link is deleted, the symbolic link will become invalid and will no longer function properly.

What is symbolic link vs hard link vs junction?

Symbolic links and hard links are both types of file that act as pointers to other files or directories, while junctions are a type of symbolic link that is specific to Windows and is used to create links between directories.

What is too many symbolic link?

“Too many symbolic link” is an error message that can occur when there are too many levels of symbolic links pointing to each other, creating an endless loop.

Do symbolic links have permissions?

Yes, symbolic links have their own permissions, which can be different from the permissions of the target file or directory.

When would you be forced to use a symbolic link rather than a normal hard link?

You would be forced to use a symbolic link rather than a normal hard link if the target file or directory is located on a different file system or partition.

Hard links can only be created within the same file system, while symbolic links can be used to reference files or directories on different file systems or partitions.

Does NTFS have symbolic links?

Yes, NTFS (New Technology File System) in Windows supports symbolic links.

What is the difference between a soft and hard link in Windows?

In Windows, a soft link is another name for a symbolic link, while a hard link is a reference to a file or directory that is created within the file system. Like in Unix-based operating systems, hard links can only be created within the same file system in Windows.

Conclusion

Symbolic links can help make our system storage spaces much more uncluttered. They also make syncing easier especially for folks who use Dropbox, Google Drive, and other applications like it.

Many apps and programs prefer their files stored somewhere specific and this sometimes causes users to have other empty memory options while a particular memory option is full.

Using these links above, you can just give the app or program the impression that the files and folders it needs are still in its preferred specific location when in reality, it’s just a hard link that’s there. 

If you have any issues creating links whether in Linux or Windows, please use the comments section below to explain your problem and I’ll get back to you with fixes.