
Linux 101 is your beginner’s guide to the Linux operating system and everything it has to offer! In this series, you’ll learn about how to customize your Linux terminal, setting up symbolic links, creating basic web pages with HTML, and much more! Check out the full list of tutorials on the Linux 101 page .
What are symbolic links?
A symbolic link, also known as a symlink, is a special type of file that points to another file or directory. Symbolic links are often used to create shortcuts or aliases to other files or directories. In most cases, symbolic links can be freely moved around within the filesystem without breaking the link and they will always point to the same destination.
It’s important to note that symlinks do not behave like Windows shortcuts; for example, if you delete the original file or directory that the symlink pointed to (the link target), then the symlink will still exist but it will now point to nothing.
Why use them?
Symbolic links are incredibly useful tools that can save you a lot of time and effort. For example, let’s say you have a file that you need to access regularly, but it’s located in a different directory. With a symbolic link, you can create a shortcut to that file in the directory where you need it.
No more having to remember where the file is located or wasting time navigating through directories! If you need to change the location of your file for any reason, all you have to do is edit the symbolic link and it will automatically update with the new location.
You can also make a symbolic link on your desktop to access frequently used files. Simply right-click on an empty space on your desktop and select Create New > Symbolic Link. After entering the path of the file you want to point to (the full path) select OK. That’s it! Now anytime you double-click this link, it will open the file without having to navigate through folders.
How do you create them?
A symbolic link, also known as a symlink, is a special type of file that points to another file or directory. Symbolic links are often used to point to frequently used files or directories, or to create shortcuts. In Linux, you can create a symbolic link using the ln command. The syntax for this command is as follows:
ln -s old_name new_name
In this example, we’ll make a shortcut for the desktop folder by creating a symbolic link named Desktop pointing to our current working directory. The syntax would be:
ln -s . Desktop doesn’t just refer to your physical desktop on your computer. You can move it anywhere on your system and it will still take you back to where you were before with all of your open windows intact. You can even share the same Desktop with other users on the same system so they have access to all of your open windows too! Now when you want to go back, just type cd then Desktop.
If there’s already an existing directory called Desktop, change the name of your new link accordingly and make sure that it is pointed at an empty folder so that no one else has access to it.
Where can you use them?
- Open a terminal window and navigate to the directory where you want to create the symbolic link.
- Use the ln command to create a symbolic link. The basic syntax for creating a symbolic link is: ln -s
- To use an existing file as a symbolic link, use its name as an argument to ln and include two hyphens (–) followed by its path and extension. For example, if you want to use /home/jdoe/file1 as a symlink for /etc/file1, then enter: ln — -s /etc/file1 /home/jdoe/file1 where jdoe is your username. If the target exists, it will be overwritten with a link pointing to it. If it does not exist yet, it will be created. If you’re using another shell than bash, or if the target does not exist at all when using bash, simply do: ln -s ~/dir1 ~/dir2
- You can also use a relative path from current working directory as shown below:
ln –relative-to=/var/www foo
- A shortcut is ln -s ../target_name ../. In this case, the link is created inside of the current directory.
- To remove a symbolic link called FILE, just enter rm FILE . Be sure that FILE is the only filename on the line. If there are other files listed on that line, such as ls -l, then you’ll need to put them on separate lines or run them through a loop first so they don’t get deleted along with the symbolic link.
An alternative way to delete a file and its associated link is with sudo rm -rf NAME but this risks accidentally deleting something else that shares the same name.
Tips and tricks
A symbolic link, also known as a symlink, is a special type of file that points to another file or directory. Symbolic links are often used to create shortcuts or aliases. When you run the ls -l command, if you see lrwxrwxrwx and something like -r–r–r– then you’re looking at a symbolic link. You can find out what it’s linking to by running ls -ld LINKNAME (replacing LINKNAME with the name of the link).
If you want to move your symbolic link somewhere else, there are two ways: First, you can use the ln command on both ends of the link (e.g., ln -s /dir1/dir2/linkname ~/Documents/newlinkname) Second, you can use rm to delete one end of the link and then recreate it using mv on the other end. For example: rm dir1/dir2/linkname && mv ~/Documents/newlinkname dir1/dir2/.