- How can I symlink a file in Linux? [closed]
- 18 Answers 18
- 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 can I symlink a file in Linux? [closed]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
I want to make a symbolic link in Linux. I have written this Bash command where the first path is the folder I want link into and the second path is the compiled source.
ln -s '+basebuild+'/IpDome-kernel/kernel /home/build/sandbox/gen2/basebuild/IpDome-kernel/kernal
18 Answers 18
To create a new symlink (will fail if symlink exists already):
ln -s /path/to/file /path/to/symlink
To create or update a symlink:
ln -sf /path/to/file /path/to/symlink
Here’s a mnemonic for you: l(i)n(k) -s(ymbolic)
I spent several minutes trying to figure out why this did not work for me. It created a self-looped link. It seems that the /path/to/file should be absolute and not relative to the «current folder». Perhaps point this out in the answer?
@AbhishekAnand it’s been a couple years, but I just wanted to leave the note that it does work with relative paths; it just needs to be relative to the resulting symbolic link’s directory and not the current directory. What you write as the first path argument is actually, verbatim, the text that’s going to be inside the symbolic link; that’s why, when it’s relative, it must be relative to the link.
Where the -s makes it symbolic.
I like to phrase it this way: ln -s where-the-symlink-should-point where-to-place-the-symlink-itself .
ln -s EXISTING_FILE_OR_DIRECTORY SYMLINK_NAME
Except it isn’t the new symlink name. It is the name or a full path (relative to cur dir or absolute) for the new symlink including the name.
ln -s EXISTING_FILE_OR_DIRECTORY
You can have a look at the man page here:
(Because an ASCII picture is worth a thousand characters.)
An arrow may be a helpful mnemonic, especially since that’s almost exactly how it looks in Emacs’ dired.
And big picture so you don’t get it confused with the Windows’ version
You could also look at these as
The from-here should not exist yet, it is to be created, while the to-here should already exist (IIRC).
(I always get mixed up on whether various commands and arguments should involve a pre-existing location, or one to be made.)
EDIT: It’s still sinking in slowly for me; I have another way I’ve written in my notes.
ln -s (target exists) (link is made) mklink (link is made) (target exists)
In Emacs’ dired , it’s super easy, as you put cursor over your target, press S , and type the directory where you’d like the link to be created. Shortens the gap between imagining the desired result and typing. See gnu.org/software/emacs/manual/html_node/emacs/….
Another mnemonic for ON: ln -s oldname newname , these words are used in the Go OS library too golang.org/pkg/os/#Symlink
ln -s source_file target_file
that is the opposite definition of «target» compared to man ln , which calls it ln -s TARGET LINK_NAME
'ln -s '+basebuild+'/IpDome-kernel/kernel /home/build/sandbox/gen2/basebuild/IpDome-kernel/kernal'
This will indeed create a symbolic link ( -s ) from the file/directory:
/home/build/sandbox/gen2/basebuild/IpDome-kernel/kernal
Here’s a few ways to help you remember:
First, there’s the man page for ln . You can access this via searching «man ln» in google, or just open a terminal window and type man ln and you’ll get the same information. The man page clearly states:
ln [OPTION]. [-T] TARGET LINK_NAME (1st form)
If having to search or read through a man page every time isn’t for you, maybe you’ll have an easier time remembering that all nix commands work the same way :
cp /file/that/exists /location/for/new/file mv /file/that/exists /location/its/moving/to ln /file/that/exists /the/new/link
cp copies a file that currently exists (the first argument) to a new file (the second argument).
mv moves a file that currently exists (the first argument) to a new place (the second argument)
Likewise ln links a file that currently exists (the first argument) to a new link (the second argument) *
The final option I would like to suggest is you can create your own man pages that are easy to read and easy (for you) to find/remember. Just make a simple shell script that gives you the hint you need. For example ♦ :
In your .bash_aliases file you can place something like:
commandsfx() < echo "Symlink: ln -s /path/to/file /path/to/symlink" echo "Copy: cp /file/to/copy /destination/to/send/copy" >alias 'cmds'=commandsfx
Then when you need it, from the command line just type cmds and you’ll get back the proper syntax in a way you can quickly read and understand it. You can make these functions as advanced as you’d like to get what what information you need, it’s up to you. You could even make them interactive so you just have to follow the prompts.. something like:
makesymlink() < echo "Symlink name:" read sym echo "File to link to:" read fil ln -s $fil $sym >alias 'symlink'=makesymlink
* — well obviously they can all take different parameters and do different things and can work on files as well as directories. but the premise is the same
♦ — examples using the bash shell
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.