X
Tech
Why you can trust ZDNET : ZDNET independently tests and researches products to bring you our best recommendations and advice. When you buy through our links, we may earn a commission. Our process

'ZDNET Recommends': What exactly does it mean?

ZDNET's recommendations are based on many hours of testing, research, and comparison shopping. We gather data from the best available sources, including vendor and retailer listings as well as other relevant and independent reviews sites. And we pore over customer reviews to find out what matters to real people who already own and use the products and services we’re assessing.

When you click through from our site to a retailer and buy a product or service, we may earn affiliate commissions. This helps support our work, but does not affect what we cover or how, and it does not affect the price you pay. Neither ZDNET nor the author are compensated for these independent reviews. Indeed, we follow strict guidelines that ensure our editorial content is never influenced by advertisers.

ZDNET's editorial team writes on behalf of you, our reader. Our goal is to deliver the most accurate information and the most knowledgeable advice possible in order to help you make smarter buying decisions on tech gear and a wide array of products and services. Our editors thoroughly review and fact-check every article to ensure that our content meets the highest standards. If we have made an error or published misleading information, we will correct or clarify the article. If you see inaccuracies in our content, please report the mistake via this form.

Close

How to use symbolic links in Linux (and why you should)

Symbolic links help to make the Linux filesystem more flexible and your user experience even simpler.
Written by Jack Wallen, Contributing Writer
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).

1. Open your terminal window

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

2. Rename the current folder

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

mv ~/Music ~/Music_OLD

3. Create the link

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.

4. Move residual files

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:

mv ~/Music_OLD/* ~/Music

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:

rm -rf ~/Music

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.

Editorial standards