How can I symlink a file in Linux? [closed]
Asked Answered
A

18

2211

I want to create a symbolic link in Linux. I have written this Bash command where the first path is the folder I want to link, and the second path is the compiled source.

ln -s '+basebuild+'/IpDome-kernel/kernel /home/build/sandbox/gen2/basebuild/IpDome-kernel/kernal 

Is this correct?

Ashford answered 23/12, 2009 at 9:50 Comment(4)
You should probably ask this question at unix.stackexchange.comPropaganda
Maybe superuser.com.Arlinda
Next time, try man ln first. It's a faster and easier way to find an answer.Eleneeleni
@DavidFerenczyRogožan I dunno, I kinda like googling the question and always finding the closed answer at the top of the results. It's like my symlink ritualMilburr
T
4068

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
Tombac answered 23/12, 2009 at 9:52 Comment(21)
@micapam The ln syntax is similar to the cp syntax, e.g. source destination.Caeoma
@micapam Is there a mnemonic to make it easier to remember? Maybe "ln -s actual link," where actual comes first alphabetically?Reveille
Here's a mnemonic for you: l(i)n(k) -s(ymbolic) <target> <optional name> (the fact that the last parameter is optional helps you figure out that it's not the target) (btw leaving out the path to the symlink creates a link in the current directory with the same basename as the target)Abhor
@micapam Just as cp is CoPy, ln is LiNk. First and third letters.Guam
Introducing... lns. lns /etc/nginx creates nginx sym link to /etc/nginx, in current directory. Install wget -O - lnsbin.info | bashJaimiejain
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?Gagger
@Abhor In the manual, it says ln [-Ffhinsv] source_file [target_file], ln [-Ffhinsv] source_file ... target_dir, link source_file target_file, so calling the first parameter target may mess with people's mind if they go to the manual later. XDBimbo
It works for directories too: ln -s /path/to/file-or-dir /path/to/symlinkSeniority
I can distinguish between file and symlink better by ln -s /path/to/destination/folder /path/to/origin/folder.Wyman
Nice learning: The second parameter is optional (under Ubuntu at least). Not providing it will make the filename of the link identical to the linked file, and create it in the current folder... (for “convenience links” this is quite handy...)Chambless
@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.Tightrope
The opposite order from Windows mklink. Sigh.Dutch
@RestlessC0bra Ik it's late, but escape the spaces by putting \'s before them.Swash
I had to add $PWD to the start of my source path to get it to work correctly.Kokura
@Burgi, only if you don't use a relative path. ie $PWD/foo/bar is the same as foo/bar. /foo/bar will not work (unless it actually is an absolute path).Sidelight
Still coming here in 2018 :)Volition
@Caeoma That is ambiguous. With cp I copy the source to destination, while with ln I don't link from source to destination, but create a link pointing to source and place the link file itself to destination.Midshipmite
pls make it clear that the destination can be a folder or file?Obryant
It's very important to notice that source and destination paths must be ABSOLUTE. I just had few issues with it.More
Use the absolute pathTechy
use -n if you want to change/update a directory symlinkNashbar
S
338
ln -s TARGET LINK_NAME

Where the -s makes it symbolic.

Shipper answered 23/12, 2009 at 9:53 Comment(3)
TARGET & LINKNAME are better way to remember. btw for delete the symlink use: unlink LINK_NAMEVolva
I like to phrase it this way: ln -s where-the-symlink-should-point where-to-place-the-symlink-itself.Midshipmite
@Volva you can delete a symlink via rm, but most people don't know to remove the trailing /.Mullin
S
328
ln -s EXISTING_FILE_OR_DIRECTORY SYMLINK_NAME
Snooperscope answered 23/12, 2009 at 9:58 Comment(4)
Better than the accepted answer and covered with unambiguous brevity.Sandra
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.Another
ln -s EXISTING_FILE_OR_DIRECTORY {optional path to/}SYMLINK_NAME and don't put a trailing slash on the symlink, as it's not a directoryKharkov
Better, understandable than @Shipper 's answerChoker
C
99
ln -s target linkName

You can have a look at the man page here:

http://linux.die.net/man/1/ln

Cutaneous answered 23/12, 2009 at 9:52 Comment(1)
good to mention "target" instead of just "file"Millman
W
98

(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

Linux:

ln -s target <- linkName

Windows:

mklink linkName -> target

You could also look at these as

ln -s "to-here" <- "from-here"
mklink "from-here" -> "to-here"

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)
Workday answered 5/5, 2014 at 18:9 Comment(4)
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/….Workday
My mnemonic is ON: ln -s Original Newfile (symlink)Malawi
this worked for mePredation
Another mnemonic for ON: ln -s oldname newname, these words are used in the Go OS library too golang.org/pkg/os/#SymlinkNewsdealer
M
25
ln -s source_file target_file

http://unixhelp.ed.ac.uk/CGI/man-cgi?ln

Molality answered 14/10, 2013 at 4:13 Comment(1)
that is the opposite definition of "target" compared to man ln, which calls it ln -s TARGET LINK_NAMEYahrzeit
C
22

To the original question:


'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:

<basebuild>/IpDome-kernel/kernel

to your new link

/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

Consonance answered 6/8, 2014 at 13:42 Comment(0)
C
19
ln [-Ffhinsv] source_file [target_file]

    link, ln -- make links

        -s    Create a symbolic link.

    A symbolic link contains the name of the file to which it is linked. 

    An ln command appeared in Version 1 AT&T UNIX.
Cooperation answered 23/12, 2009 at 9:56 Comment(0)
O
19

Creating Symbolic links or Soft-links on Linux:

Open Bash prompt and type the below mentioned command to make a symbolic link to your file:

A) Goto the folder where you want to create a soft link and typeout the command as mentioned below:

$ ln -s (path-to-file) (symbolic-link-to-file)
$ ln -s /home/user/file new-file

B) Goto your new-file name path and type:

$ ls -lrt (To see if the new-file is linked to the file or not)

Example:

user@user-DT:[~/Desktop/soft]# ln -s /home/user/Desktop/soft/File_B /home/user/Desktop/soft/File_C
user@user-DT:[~/Desktop/soft]# ls -lrt
total 0
-rw-rw-r-- 1 user user  0 Dec 27 16:51 File_B
-rw-rw-r-- 1 user user  0 Dec 27 16:51 File_A
lrwxrwxrwx 1 user user 31 Dec 27 16:53 File_C -> /home/user/Desktop/soft/File_B


Note: Where, File_C -> /home/user/Desktop/soft/File_B  Means, File_C is symbolically linked to File_B
Oscillograph answered 6/11, 2016 at 18:32 Comment(0)
R
13

This is Stack Overflow so I assume you want code:

All following code assumes that you want to create a symbolic link named /tmp/link that links to /tmp/realfile.

CAUTION: Although this code checks for errors, it does NOT check if /tmp/realfile actually exists ! This is because a dead link is still valid and depending on your code you might (rarely) want to create the link before the real file.


Shell (bash, zsh, ...)

#!/bin/sh
ln -s /tmp/realfile /tmp/link

Real simple, just like you would do it on the command line (which is the shell). All error handling is done by the shell interpreter. This code assumes that you have a working shell interpreter at /bin/sh .

If needed you could still implement your own error handling by using the $? variable which will only be set to 0 if the link was successfully created.

C and C++

#include <unistd.h>
#include <stdio.h>

int main () {
  if( symlink("/tmp/realfile", "/tmp/link") != 0 )
    perror("Can't create the symlink");
}

symlink only returns 0 when the link can be created. In other cases I'm using perror to tell more about the problem.

Perl

#!/usr/bin/perl
if( symlink("/tmp/realfile", "/tmp/link") != 1) {
  print STDERR "Can't create the symlink: $!\n"
}

This code assumes you have a perl 5 interpreter at /usr/bin/perl. symlink only returns 1 if the link can be created. In other cases I'm printing the failure reason to the standard error output.

Reductive answered 1/2, 2018 at 21:11 Comment(1)
Explanation added, also a word of caution for dead links. I'm not checking for this, because it can't be assumed that this is not wanted and also because filetests are no longer on topicReductive
R
12

ln -s sourcepath linkpathname

Note:

-s makes symbolic links instead of hard links

Rusell answered 11/5, 2015 at 7:34 Comment(0)
W
11

If you are in the directory where you want to create symlink, then ignore second path.

cd myfolder
ln -s target

It will create symlink of target inside myfolder.

General syntax

ln -s TARGET LINK_NAME
Wyon answered 16/4, 2014 at 10:56 Comment(0)
P
8

I'd like to present a plainer-English version of the descriptions already presented.

 ln -s  /path-text/of-symbolic-link  /path/to/file-to-hold-that-text

The "ln" command creates a link-FILE, and the "-s" specifies that the type of link will be symbolic. An example of a symbolic-link file can be found in a WINE installation (using "ls -la" to show one line of the directory contents):

 lrwxrwxrwx 1 me power 11 Jan  1 00:01 a: -> /mnt/floppy

Standard file-info stuff is at left (although note the first character is an "l" for "link"); the file-name is "a:" and the "->" also indicates the file is a link. It basically tells WINE how Windows "Drive A:" is to be associated with a floppy drive in Linux. To actually create a symbolic link SIMILAR to that (in current directory, and to actually do this for WINE is more complicated; use the "winecfg" utility):

 ln -s  /mnt/floppy  a:   //will not work if file a: already exists
Photojournalism answered 4/4, 2014 at 14:19 Comment(0)
N
7

To create a symbolic link /soft link, use:

ln -s {source-filename} {symbolic-filename}

e.g.:

ln -s file1 link1
Neocene answered 10/7, 2017 at 19:30 Comment(0)
O
5

Links are basically of two types:

Symbolic links (soft): link to a symbolic path indicating the abstract location of another file

Hard links: link to the specific location of physical data.

Example 1:

ln /root/file1 /root/file2

The above is an example of a hard link where you can have a copy of your physical data.

Example 2:

ln -s /path/to/file1.txt /path/to/file2.txt

The above command will create a symbolic link to file1.txt.

If you delete a source file then you won't have anything to the destination in soft.

When you do:

ls -lai

You'll see that there is a different inode number for the symlinks.

For more details, you can read the man page of ln on your Linux OS.

Opprobrious answered 6/12, 2018 at 1:53 Comment(1)
hard link where you can have a copy of your physical data - I think we don't create copy of physical data. Just another name for the same physical data.Mistranslate
S
3

There are two types of links:

symbolic links: Refer to a symbolic path indicating the abstract location of another file

hard links: Refer to the specific location of physical data.

In your case symlinks:

ln -s source target

you can refer to http://man7.org/linux/man-pages/man7/symlink.7.html

you can create too hard links

A hard link to a file is indistinguishable from the original directory entry; any changes to a file are effectively independent of the name used to reference the file. Hard links may not normally refer to directories and may not span file systems.

ln source link
Schulze answered 27/6, 2017 at 17:55 Comment(0)
M
3

I find a bit confusing the terminologies "target" and "directory" in the man information.

The target is the folder that we are symlinking to and the directory the actual symlink (not the directory that you will be symlinking to), if anyone is experiencing the same confusion, don't feel alone.

This is my interpretation of creating a Symlink (in linux):

ln -s /FULL/PATH/FOLDER-OR-FILE-SYMLINKING-TO NAME-OF-YOUR-SYMLINK

You can navigate to the folder where you want to create the symlink and run the command or specify the FULL PATH for your symlink instead of NAME-OF-YOUR-SYMLINK.

cd /FULL/PATH/TO/MY-SYMLINK-PARENT-FOLDER
ln -s /FULL/PATH/FOLDER-OR-FILE-SYMLINKING-TO NAME-OF-YOUR-SYMLINK

OR

ln -s /FULL/PATH/FOLDER-OR-FILE-SYMLINKING-TO /FULL/PATH/TO/MY-SYMLINK-PARENT-FOLDER/NAME-OF-YOUR-SYMLINK

I hope this helps to those (still) slighly confused.

Mcelroy answered 16/11, 2017 at 10:35 Comment(0)
F
-2

How to create symlink in vagrant. Steps:

  1. In vagrant file create a synced folder. e.g config.vm.synced_folder "F:/Sunburst/source/sunburst/lms", "/source" F:/Sunburst/source/sunburst/lms :- where the source code, /source :- directory path inside the vagrant
  2. Vagrant up and type vagrant ssh and go to source directory e.g cd source
  3. Verify your source code folder structure is available in the source directory. e.g /source/local
  4. Then go to the guest machine directory where the files which are associate with the browser. After get backup of the file. e.g sudo mv local local_bk
  5. Then create symlink e.g sudo ln -s /source/local local. local mean link-name (folder name in guest machine which you are going to link) if you need to remove the symlink :- Type sudo rm local
Filings answered 15/2, 2016 at 9:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.