A sample of my Music folder listing.

Jack Wallen/ZDNET

Linux has a lot of tricks up its sleeve. One such trick makes the filesystem far more flexible than you might imagine. That feature is called symbolic links, also known as symlinks.

To explain how this feature works, let’s use an example of a particular folder in your home directory. The folder in question is Music. The Music folder is typically created by default in most modern Linux distributions and serves exactly the purpose you think — housing music files.

Also: The best Linux distros for beginners: You can do this!

I have a drive that houses a vast collection of digital music files I’ve had for years that’s been transferred from drive to drive as needed. If I were to copy all those files and sub-folders to the Music directory on the drive housing my operating system, I would find myself in trouble because that drive would instantly become full.

Of course, you can access all those music files from the drive where they reside. In my case, that would be /media/jack/OLIVIA/Music (I name drives and hostnames after characters in my books). That link works but it’s not nearly as efficient as ~/Music. Therefore, I create a symlink to simplify things.

Essentially, a symlink points from one directory to another. Let’s continue with our Music example. If I create a symlink in my /home/jack directory that points to the target /media/jack/OLIVIA/Music, any file I add to ~/Music gets saved in the target directory. 

Also: Sparky Linux is a blazing-fast distro that can keep your older machines running for years 

Any file I add to the target directory is automatically available via the symlink. In other words, what you see in ~/Music is the same as in /media/jack/OLIVIA/Music. The only difference is that the files only exist in the latter location.

There are two types of symbolic links:

  • Hard links – Points to the inode of a file, instead of pointing to the file itself.
  • Soft links – Points to a file.

We’ll focus on soft links because that is the symlink most users will need.

How to create a symlink in Linux

What you’ll need: The only thing you’ll need for this trick is a running instance of Linux, as every distribution can work with symbolic links.

Also: Thinking about switching to Linux? 10 things you need to know

I’ll demonstrate this process with the example above (linking ~/Music to another partition).

Log in to your operating system and open the default terminal window application.

Rename the current Music folder in your home directory. To do that, issue the command:

Next, we’ll create the link. Remember, I’m creating a link to /media/jack/OLIVIA/Music and your link will point to a different folder on your file system. For my link, the command would be:

ln -s /media/jack/OLIVIA/Music ~/Music

Notice that we don’t have to create the ~/Music directory. If we did create a ~/Music directory, the symlink would appear within that directory and be ~/Music/Music. We don’t want that to happen, so we’ll let the ln command do the work.

If you’ve saved any music files within the original ~/Music folder, you will want to move the files to the new linked directory, which will move them to the target, with the command:

Now, everything should be in one location and not on the same drive housing your OS. 

That approach means you won’t run out of room and cause the operating system to lock up.

Also: There’s a new coolest Linux distribution ready to wow you 

If, at any point, you want to remove a symlink, you would do so in the same way you’d remove a folder. In the case of our example, that would be:

When you remove the symlink, you don’t harm the target directory. 

And that’s all there is to using symbolic links in Linux. Use these links to make interacting with your file system more efficient.


Source link