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
Ln Command: How to Create Symbolic Links in Linux
A link creates a reference to a file or a folder. Symbolic links are used in Linux for managing and collating files.
In this guide, learn how to use the ln command to create symbolic links in Linux.
- A system running Linux
- Access to a terminal window / command line (Activities >Search> type Terminal)
- (optional) A user account with sudo or root privileges (needed to access certain protected files and directories)
Ln Command to Create Symbolic Links
To use the ln command, open a terminal window and enter the command with the following format:
- By default, the ln command creates a hard link.
- Use the -s option to create a soft (symbolic) link.
- The -f option will force the command to overwrite a file that already exists.
- Source is the file or directory being linked to.
- Destination is the location to save the link – if this is left blank, the symlink is stored in the current working directory.
For example, create a symbolic link with:
ln -s test_file.txt link_file.txt
This creates a symbolic link (link_file.txt) that points to the test_file.txt.
To verify whether the symlink has been created, use the ls command:
Create a Symbolic Link to Linux Directory
A symbolic link can refer to a directory. To create a symbolic link to a directory in Linux:
ln -s /mnt/external_drive/stock_photos ~/stock_photos
This example creates a symbolic link named stock_photos in the home (~/) directory. The link refers to the stock_photos directory on an external_drive.
Note: If the system has a connection to another computer, such as a corporate network or a remote server, symlinks can be linked to resources on those remote systems.
Force Overwrite Symbolic Links
You might receive an error message as displayed in the image below:
The error message means that there’s already a file in the destination named link_file.txt. Use the -f option to force the system to overwrite the destination link:
ln -sf test_file.txt link_file.txt
Note: Using the -f option will permanently delete the existing file.
Deleting or Removing Links
If the original file is moved, deleted, or becomes unavailable (such as a server going offline), the link will be unusable. To remove a symbolic link, use either the rm (remove) or unlink command:
rm link_file.txt unlink link_file.txt
Soft Links vs Hard Links
The ln command can be used to create two different kinds of links:
Soft (Symbolic) Links
A soft link, sometimes called a symbolic link or symlink, points to the location or path of the original file. It works like a hyperlink on the internet.
Here are a few important aspects of a soft link:
- If the symbolic link file is deleted, the original data remains.
- If the original file is moved or deleted, the symbolic link won’t work.
- A soft link can refer to a file on a different file system.
- Soft links are often used to quickly access a frequently-used file without typing the whole location.
Hard Links
When a file is stored on a hard drive, several things happen:
- The data is physically written to the disk.
- A reference file, calledinode, is created to point to the location of the data.
- A filename is created to refer to the inode data.
A hard link works by creating another filename that refers to the inode data of the original file. In practice, this is similar to creating a copy of the file.
Here are a few important aspects of hard links:
- If the original file is deleted, the file data can still be accessed through other hard links.
- If the original file is moved, hard links still work.
- A hard link can only refer to a file on the same file system.
- The inode and file data are permanently deleted when the number of hard links is zero.
You should now have a solid understanding of hard and symbolic (soft) links, and how to work with them. Use the ln command to create links and verify using the ls command.