soft - How to symlink a file in Linux?

hard ubuntu (15)

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 

Is this correct?


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


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.




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



Where the -s makes it symbolic.

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

ln -s sourcepath linkpathname


-s makes symbolic links instead of hard links

(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


ln -s target <- linkName


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)

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

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

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

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.

To create a symbolic link /soft link, use:

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


ln -s file1 link1

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

This is 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, ...)

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.


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.

Running shell scripts that have contain sudo commands in them from jenkins might not run as expected. To fix this, follow along

Simple steps:

  1. On ubuntu based systems, run " $ sudo visudo "

  2. this will open /etc/sudoers file.

  3. If your jenkins user is already in that file, then modify to look like this:


  1. save the file

  2. Relaunch your jenkins job

  3. you shouldnt see that error message again :)

linux symlink