- How to create a soft or symbolic link?
- 8 Answers 8
- Symlink Tutorial in Linux – How to Create and Remove a Symbolic Link
- Difference Between a Soft Link and a Hard Link
- How to Create a Symlink
- How to Create a Symlink for a File – Example Command
- How to Create a Symlink for a Folder – Example Command
- How to remove a symlink
- How to Use Unlink to Remove a Symlink
- How to use rm to Remove a Symlink
- How to Find and Delete Broken Links
- Wrapping up
- How to Symlink a Directory in Linux
- How to Create Symlink (soft-link) in Linux
- How to Create a Symlink (soft link) to a File
- How to Create a Symlink (Soft Link) of the Folder/Directory
- How to Overwrite the Symlink (Soft Link) in Linux:
- How to Remove Symlink (Soft Link) in Linux:
- Conclusion
- About the author
- Sam U
How to create a soft or symbolic link?
I am installing p4v in /opt , but /usr/bin is on my path. Is it possible to create a soft or symbolic link for p4v from /opt to /usr/bin , so I can just type «p4v» since /usr/bin is in my path?
8 Answers 8
To create a symlink at /usr/bin/bar which references the original file /opt/foo , use:
You would need to apply the above command as root (i.e. with sudo ).
I am using: sudo ln –s /etc/apache2/sites-available/redmine /etc/apache2/sites-enabled/000-redmine getting error: ln: target ‘/etc/apache2/sites-enabled/000-redmine’ is not a directory
The Ubuntu documentation says » Creates hard links by default, symbolic links with —symbolic.» Will the above solution create a symbolic link as asked by OP?
I though César wanted to put his files in the /opt and /usr/bin to have the symbolic link, not other way around.
@kevinmicke after your explanation finally realized that the explanation of the answer was stated in reverse order from the command, making my brain (and others’) read it backwards
The error is that you are writing the command wrong. The correct way is
If the ‘p4v’ executable is at /opt/bin/p4v, you can simply run:
sudo ln -s /opt/bin/p4v /usr/bin/p4v sudo chmod ugo+x /usr/bin/p4v
It would be better to add /opt/bin (or wherever the executable is) to your path:
echo "export PATH=\$PATH:/opt/bin" >> ~/.profile reset
Check the software location by this.
which application-name #replace for the application you are looking for
To create the soft link. for example you want to create the soft link for skype on your desktop
For more information about ln .
This template was more helpful for me than the above answers. Probably not more correct, just less obfuscated:
Just replace the parts in <> ‘s
ln -s -n ./TargetDirectory ./Nickname
Note, this works if you both nodes are below you in the same tree. You can use relative notation
- -s command makes it a symbolic link
- -n makes it possible de create a folder-type symlink
Welcome to askubuntu.com. In this case the $ to indicate a command line prompt is a style choice, and not likely to be a problem. However bear in mind that including things in a code block other than the code and its output can cause confusion.
If it is saying target is not a folder , it means there are spaces in your folder names eg: New Folder has a space
You need to edit the path and add a backslash \ after every space in the paths
ln -s /opt/bin /usr/var/New\ Folder
This is not an answer to the OPs question. Please wait until you have enough reputation to add comments.
I have found that it is easier to go to where you want the link to be and then create the link using sudo ln -s /path/to/source/file , than doing ln -s target source .
So in your case I would do cd /usr/bin then sudo ln -s /opt/bin/pv4 . The other way has not been working in my case.
Symlink Tutorial in Linux – How to Create and Remove a Symbolic Link
Dillion Megida
A symlink (also called a symbolic link) is a type of file in Linux that points to another file or a folder on your computer. Symlinks are similar to shortcuts in Windows.
Some people call symlinks «soft links» – a type of link in Linux/UNIX systems – as opposed to «hard links.»
Difference Between a Soft Link and a Hard Link
Soft links are similar to shortcuts, and can point to another file or directory in any file system.
Hard links are also shortcuts for files and folders, but a hard link cannot be created for a folder or file in a different file system.
Let’s look at the steps involved in creating and removing a symlink. We’ll also see what broken links are, and how to delete them.
How to Create a Symlink
The syntax for creating a symlink is:
ln is the link command. The -s flag specifies that the link should be soft. -s can also be entered as -symbolic .
By default, ln command creates hard links. The next argument is path to the file (or folder) that you want to link. (That is, the file or folder you want to create a shortcut for.)
And the last argument is the path to link itself (the shortcut).
How to Create a Symlink for a File – Example Command
ln -s /home/james/transactions.txt trans.txt
After running this command, you will be able to access the /home/james/transactions.txt with trans.txt . Any modification to trans.txt will also be reflected in the original file.
Note that this command above would create the link file trans.txt in your current directory. You can as well create a linked file in a folder link this:
ln -s /home/james/transactions.txt my-stuffs/trans.txt
There must be a directory already called «my-stuffs» in your current directory – if not the command will throw an error.
How to Create a Symlink for a Folder – Example Command
This would create a symlinked folder called ‘james’ which would contain the contents of /home/james . Any changes to this linked folder will also affect the original folder.
How to remove a symlink
Before you’d want to remove a symlink, you may want to confirm that a file or folder is a symlink, so that you do not tamper with your files.
Running this command on your terminal will display the properties of the file. In the result, if the first character is a small letter L (‘l’), it means the file/folder is a symlink.
You’d also see an arrow (->) at the end indicating the file/folder the simlink is pointing to.
There are two methods to remove a symlink:
How to Use Unlink to Remove a Symlink
This deletes the symlink if the process is successful.
Even if the symlink is in the form of a folder, do not append ‘/’, because Linux will assume it’s a directory and unlink can’t delete directories.
How to use rm to Remove a Symlink
As we’ve seen, a symlink is just another file or folder pointing to an original file or folder. To remove that relationship, you can remove the linked file.
Note that trying to do rm james/ would result an error, because Linux will assume ‘james/’ is a directory, which would require other options like r and f . But that’s not what we want. A symlink may be a folder, but we are only concerned with the name.
The main benefit of rm over unlink is that you can remove multiple symlinks at once, like you can with files.
How to Find and Delete Broken Links
Broken links occur when the file or folder that a symlink points to changes path or is deleted.
For example, if ‘transactions.txt’ moves from /home/james to /home/james/personal , the ‘trans.txt’ link becomes broken. Every attempt to access to the file will result in a ‘No such file or directory’ error. This is because the link has no contents of its own.
When you discover broken links, you can easily delete the file. An easy way to find broken symlinks is:
This will list all broken symlinks in the james directory – from files to directories to sub-directories.
Passing the -delete option will delete them like so:
find /home/james -xtype l -delete
Wrapping up
Symbolic link are an interesting feature of Linux and UNIX systems.
You can create easily accessible symlinks to refer to a file or folder that would otherwise not be convenient to access. With some practice, you will understand how these work on an intuitive level, and they will make you much more efficient at managing file systems.
Dillion Megida
Developer Advocate and Content Creator passionate about sharing my knowledge on Tech. I simplify JavaScript / ReactJS / NodeJS / Frameworks / TypeScript / et al My YT channel: youtube.com/c/deeecode
If you read this far, tweet to the author to show them you care. Tweet a thanks
Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started
freeCodeCamp is a donor-supported tax-exempt 501(c)(3) charity organization (United States Federal Tax Identification Number: 82-0779546)
Our mission: to help people learn to code for free. We accomplish this by creating thousands of videos, articles, and interactive coding lessons — all freely available to the public. We also have thousands of freeCodeCamp study groups around the world.
Donations to freeCodeCamp go toward our education initiatives, and help pay for servers, services, and staff.
How to Symlink a Directory in Linux
Symlink, also known as a symbolic link in Linux, creates a link to a file or a directory for easier access. To put it in another way, symlinks are links that points to another file or folder in your system, quite similar to the shortcuts in Windows. Some users refer to symlinks as soft-links. Before moving forward, let’s elaborate soft-links and hard-links.
Hard-links: Hard-links are the links that mirror or copy the original file. Hard-links have the same inode numbers.
Soft-links: Soft-links are simple links that points to the original file. You can access the original file through soft links. Soft-links can point to a file or folder in any partition and have different inode numbers.
Learning about creating symlink in Linux is a great way to improve your grip on the Linux terminal. So, let’s learn the steps involved in making the soft-links in Linux.
How to Create Symlink (soft-link) in Linux
To make symlink or soft link, we use the “ln” command. The syntax to follow to create symlink is mentioned below:
In the first argument after the “-s” option, you will be giving the path of the file of a folder you want to create the symlink of. While in the second argument, pass the name you want to give that symlink. To check the created links, use the following command:
To check inode numbers, use the command mentioned below:
How to Create a Symlink (soft link) to a File
Creating a soft link to a file is simple; use the syntax mentioned below:
Important to note that if you do not specify the “[symbolic name]”, then the command will create a symlink by the original file’s name. Let’s understand it through an example.
I have created a directory “my_folder” that contains a text file “my_doc.txt”. Now, to create symlink to “my_doc.txt” file, I will use:
As it can be seen in the above output, “my_document” is pointing to “my_folder/my_doc.txt” file. Both the symlink and the original file would have different inode number. To check inode numbers used:
Hard links will always have same inode numbers. To verify, I created a hard link of “my_doc.txt” file and name it “my_document_2”:
It can be seen in the output that the original file and the hard link have same inode numbers.
How to Create a Symlink (Soft Link) of the Folder/Directory
To create a soft-link or symlink to a directory is quite similar to creating a symlink to a file. For instance, I am creating the symlink of the “my_folder” directory using:
The above command will create a symlinked folder in the current directory. To verify it, use:
How to Overwrite the Symlink (Soft Link) in Linux:
If you try to update a symlink with the same name that already exist, then you will get an error:
We will have to use the force flag “-f” to overwrite the new path to the existing symlink.
How to Remove Symlink (Soft Link) in Linux:
In many situation, you need to remove the unnecessary symlinks from your system. To delete symlink, we use the “unlink” command, and the syntax is given below:
Let’s remove the symlinks we created in the above examples. To unlink a symlink of a file, use:
And to unlink the symlink of a directory:
We can also use the “rm” command to remove symlinks.
The advantage of “rm” over “unlink” is that you can remove multiple symlinks with the “rm” command, which is not possible with the “unlink” command as shown in the following image:
Note that whether you use the “unlink” or “rm” command, do not use trailing slash “/” even if it is a directory.
Conclusion
Symlinks are an easier way to access the files of your system from multiple locations. This write-up is a thorough guide about creating symlinks to a file or directory and removing them. Remove the symlinks if the original file no longer exists.
Understanding and mastering the Linux terminal is very crucial for any beginner. I hope this post benefitted you to learn a new utility and improve your skills.
About the author
Sam U
I am a professional graphics designer with over 6 years of experience. Currently doing research in virtual reality, augmented reality and mixed reality.
I hardly watch movies but love to read tech related books and articles.