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?
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:
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.
http://unixhelp.ed.ac.uk/CGI/man-cgi?ln
To create a new symlink (will fail if symlink exists already):
To create or update a symlink:
I'd like to present a plainer-English version of the descriptions already presented.
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):
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):
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, ...)
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++
symlink
only returns 0 when the link can be created. In other cases I'm usingperror
to tell more about the problem.Perl
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.