- How to create a link in Linux
- How to create symbolic links using a file manager
- How to create symbolic links from the command line
- Linux link command
- Description
- Syntax
- Options
- Examples
- Related commands
- Linux ln command
- Description
- What is a link?
- Using the link command
- The difference between ln and link
- What are symbolic links?
- Creating symlinks to directories
- Syntax
- Options
- The —backup option
- Examples
- Related commands
How to create a link in Linux
In your Linux file system, a link is a connection between a file name and the actual data on the disk. There are two main types of links that can be created: «hard» links, and «soft» or symbolic links. Hard links are low-level links which the system uses to create elements of the file system itself, such as files and directories.
Most users do not want to create or modify hard links themselves, but symbolic links are a useful tool for any Linux user. A symbolic link is a special file that points to another file or directory, which is called the target. Once created, a symbolic link can be used in place of the target file name. It can have a unique name, and be located in any directory. Multiple symbolic links can even be created to the same target file, allowing the target to be accessed by multiple names.
The symbolic link is a file in its own right, but it does not contain a copy of the target file’s data. It is similar to a shortcut in Microsoft Windows: if you delete a symbolic link, the target is unaffected. Also, if the target of a symbolic link is deleted, moved, or renamed, the symbolic link is not updated. When this happens, the symbolic link is called «broken» or «orphaned,» and will no longer function as a link.
How to create symbolic links using a file manager
One way to create a symbolic link in the X Windows GUI is with your file manager. Some Linux distributions use different file managers, but the process is similar. Locate a target file in your file manager GUI, highlight it by clicking it once, and select the «create a link» option. This option is usually found under the Edit menu, or in the context menu that appears when you right-click the highlighted file.
In the example shown above, using the Thunar file manager, we have highlighted the file myfile.txt, then selected Make Link in the Edit menu. After completed, a new symbolic link called link to myfile.txt is created. This link can be renamed or moved to another location. It always points to the target, unless the target is later moved or deleted, resulting in the link becoming orphaned.
How to create symbolic links from the command line
The command line is a powerful tool in Linux because it gives you greater control over your commands. (For more information about the command line, and how to access it from Linux, see our Linux and Unix shell tutorial).
You can create symbolic links using the ln command’s -s option. The general syntax for creating a symbolic link is:
For instance, if we have a file in our working directory called myfile.txt, and we want to create a symbolic link in the same directory called mylink, we could use the command:
In this command, we have opened a terminal session that places us at our shell’s command prompt. We are logged in a system named myhost as a user named user, and our working directory is a folder in our home directory called myfolder:
First, let’s use ls with the -l option to produce a long list of all the files in our directory:
We see our file, myfile.txt, which is the only file in the directory. («total 4» refers to how many blocks on the disk are used by the files listed, not the total number of files).
Let’s use the cat command to view the contents of myfile.txt:
Now, let’s create a symbolic link to mylink.txt called mylink using the ln -s command:
It seems like nothing happened, but this means it worked as expected. If there was an error, or if an unexpected condition was encountered, we would receive a notification.
Now, if we do another ls -l, we see two files — our target and our link:
One of the benefits of doing a long listing with «-l» is that we see extra information in addition to the file name. Notice the «l» at the beginning of the line containing our link name, indicating that the file is a symbolic link. Also, after mylink (in blue text) is the «->» symbol, followed by the name of the target.
Most shells, by default, are configured to display certain file types in different colors, but your terminal might show different colors or none at all.
Now, let’s use our symbolic link. If we run cat on it, it displays the contents of myfile.txt:
We can rename our link with mv, and it still points to the same target:
But what happens if we move our link somewhere else? In this case, our link breaks. We can see this by making a new directory using mkdir, and moving the link into the new directory using mv:
You can see that when we view the contents of directory newfolder with ls -l, our link is highlighted in red, indicating that it is a broken link. If we try to cat the contents of the link, the shell informs us that the file does not exist. It points to «myfile.txt» with no other path information. Therefore, the operating system looks for myfile.txt in the same directory as the link.
Let’s start over by removing newfolder and its contents using the command rm -r:
This time, let’s create the symbolic link using the absolute path to myfile.txt. Let’s double check the name of our working directory using pwd:
Our working directory is /home/user/myfolder, so let’s include this in the target name when we create the link:
As you can see from the output of ls -l, our link now points to the file /home/user/myfolder/myfile.txt. With this path information, we can move the link to another location, and it still points to our target:
Your bash shell keeps an environment variable called $PWD that always stores the value of your working directory. You can use this variable to insert the full path before your target name, as long as the target is in your working directory. We can view the value of $PWD using the echo command:
This text is inserted if we use $PWD as part of a command. It is a good idea to enclose it in quotes as «$PWD» in case the directory name has any spaces. The quotes make sure the shell knows they are part of the pathname and not command separators.
Here is our command, and a directory listing to show that it worked:
Источник
Linux link command
On Unix-like operating systems, The link command associates a file with a file name in a file system.
This page describes the GNU/Linux version of link.
Description
The link command creates a hard link named FILE2, which shares the same index node as the existing file FILE1. Since FILE1 and FILE2 share the same index node, they point to the same data on the disk, and modifying one is functionally the same as modifying the other.
This is distinct from creating a «soft» symbolic link to a file, which creates its own index node and does not directly point to the same data.
For example, a user cannot create a hard link which links to a directory, but this can be accomplished using a symbolic link.
Syntax
Options
—help | Display help and exit. |
—version | Display version information, and exit. |
Examples
The example above would create the file hope.txt linked to the file computer.txt. Any changes that occurred with either of these files would affect the other file or link.
Related commands
ln — Create a link, or a symbolic link, to a file or directory.
ls — List the contents of a directory or directories.
unlink — Remove a file.
Источник
Linux ln command
On Unix-like operating systems, the ln command creates links between files, associating file names with file data.
This page covers the GNU/Linux version of ln.
Description
ln creates a link to file TARGET with the name LINKNAME. If LINKNAME is omitted, a link to TARGET is created in the current directory, using the name of TARGET as the LINKNAME.
ln creates hard links by default, or symbolic links if the -s (—symbolic) option is specified. When creating hard links, each TARGET must exist.
What is a link?
Before we discuss the ln command, let’s first discuss the link command, what a link is, and how it relates to files as we know them.
A link is an entry in your file system which connects a file name to the actual bytes of data on the disk. More than one file name can «link» to the same data. Here’s an example. Let’s create a file named file1.txt:
This command echoes the string «This is a file«. Normally this would echo to our terminal, but the > operator redirects the string’s text to a file, in this case file1.txt. We can check that it worked using cat to display the contents of the file:
When this file was created, the operating system wrote the bytes to a location on the disk and also linked that data to a file name, file1.txt so that we can refer to the file in commands and arguments. If you rename the file, the contents of the file are not altered; only the information that points to it. The file name and the file’s data are two separate entities.
Here’s an illustration of the file name and the data to help you visualize it:
Using the link command
What the link command does is allow us to manually create a link to file data that already exists. So, let’s use link to create our own link to the file data recently created. In essence, we’ll create another file name for the data that already exists.
Let’s call our new link file2.txt. How do we create it?
The general form of the link command is: «link file_name linkname«. Our first argument is the name of the file whose data we’re linking to; the second argument is the name of the new link we’re creating.
Now both file1.txt and file2.txt point to the same data on the disk:
The important thing to realize is that we did not make a copy of this data. Both file names point to the same bytes of data on the disk. Here’s an illustration to help you visualize it:
If we change the contents of the data pointed to by either one of these files, the other file’s contents are changed as well. Let’s append a line to one of them using the >> operator:
Now let’s look at the contents of file1.txt:
. and now let’s look at the second file, the one we created with the link command:
Both files show the change because they share the same data on the disk. Changes to the data of either one of these files change the contents of the other.
But what if we delete one of the files? Will both files be deleted?
No. If we delete one of the files, we’re deleting one of the links to the data. Because we created another link manually, we still have a pointer to that data; we still have a way, at the user-level, to access the data we put in there. So if we use the rm command to remove our first file:
. it no longer exists as a file with that name:
. but the link to the data we manually created still exists, and still points to the data:
As you can see, the data stays on the disk even after the «file» (which is actually a link to the data) is removed. We can still access that data as long as there is a link to it. This is important to know when you’re removing files — «removing» a file makes the data inaccessible by unlink-ing it. The data still exists on the storage media, somewhere, inaccessible to the system, and that space on disk is marked as being available for future use.
The type of link we’ve been working with here is sometimes called a «hard» link. A hard link and the data it links to must always exist on the same filesystem; you can’t, for instance, create a hard link on one partition to file data stored on another partition. You also can’t create a hard link to a directory. Only symbolic links may link to a directory; we’ll get to that in a moment.
The difference between ln and link
So what about ln? That’s why we’re here, right?
ln, by default, creates a hard link like link does. So this ln command:
. is the same as the following link command:
. because both commands create a hard link named file2.txt which links to the data of file1.txt.
However, we can also use ln to create symbolic links with the -s option. So the command:
Create a symbolic link to file1.txt named file2.txt. In contrast to our hard link example, here’s an illustration to help you visualize our symbolic link:
What are symbolic links?
Symbolic links, sometimes called «soft» links, are different than «hard» links. Instead of linking to the data of a file, they link to another link. So in the example above, file2.txt points to the link file1.txt, which in turn points to the data of the file.
This has several potential benefits. For one thing, symbolic links (also called «symlinks» for short) can link to directories. Also, symbolic links can cross file system boundaries, so a symbolic link to data on one drive or partition can exist on another drive or partition.
You should also be aware that, unlike hard links, removing the file (or directory) that a symlink points to breaks the link. So if we create file1.txt:
. and create a symbolic link to it:
. we can cat either one of these to see the contents:
. but if we remove file1.txt:
. we can no longer access the data it contained with our symlink:
This error message might be confusing at first, because file2.txt still exists in your directory. It’s a broken symlink, however — a symbolic link which points to something that no longer exists. The operating system tries to follow the symlink to the file that’s supposed to be there (file1.txt), but finds nothing, and so it returns the error message.
While hard links are an essential component of how the operating system works, symbolic links are generally more of a convenience. You can use them to refer, in any way you’d like, to information already on the disk somewhere else.
Creating symlinks to directories
To create a symbolic link to a directory, specify the directory name as the target. For instance, let’s say we have a directory named documents, which contains one file, named file.txt.
Let’s create a symbolic link to documents named dox. This command will do the trick:
We now have a symlink named dox which we can refer to as if it’s the directory documents. For instance, if we use ls to list the contents of the directory, and then to list the contents of the symlinked directory, they will both show the same file:
When we work in the directory dox now, we are actually working in documents, but we see the word dox instead of documents in all pathnames.
Symbolic links are a useful way to make shortcuts to long, complicated pathnames. For instance, this command:
. saves us a lot of typing; now, instead of changing directory with the following command:
. we can do this, instead:
Normally, you remove directories (once they’re empty) with the rmdir command. But our symbolic link is not actually a directory: it’s a file that points to a directory. So to remove our symlink, we use the rm command:
This command removes the symlink, but the original directory and all its files are not affected.
Syntax
Options
Here are the options that can be passed to the ln command.
—backup[=CONTROL] | Use this option to additionally create a backup of each existing destination file. The style of backup is optionally defined by the value of CONTROL. See backup options below for more information. |
-b | This functions like —backup, but you cannot specify the CONTROL; the default style (simple) is used. |
-d, -F, —directory | This option allows the superuser to attempt to hard link directories (although it will probably fail due to system restrictions, even for the superuser). |
-f, —force | If the destination file or files already exist, overwrite them. |
-i, —interactive | Prompt the user before overwriting destination files. |
-L, —logical | Dereference TARGETs that are symbolic links. In other words, if you are trying to create a link (or a symlink) to a symlink, link to what it links to, not to the symlink itself. |
-n, —no-dereference | Treat LINKNAME as a normal file if it’s a symbolic link to a directory. |
-P, —physical | Make hard links directly to symbolic links, rather than dereferencing them. |
-r, —relative | Create symbolic links relative to link location. |
-s, —symbolic | Make symbolic links instead of hard links. |
-S, —suffix=SUFFIX | Use the file suffix SUFFIX rather than the default suffix « «. |
-t, —target-directory=DIRECTORY | Specify the DIRECTORY to create the links. |
-T, —no-target-directory | Always treat LINKNAME as a normal file. |
-v, —verbose | Operate verbosely; print the name of each linked file. |
—help | Display a help message, and exit. |
—version | Display version information, and exit. |
The —backup option
When using the —backup (or -b) option, the default file suffix for backups is ‘
‘. You can change this, however, using the —suffix option or setting the SIMPLE_BACKUP_SUFFIX environment variable.
The CONTROL argument to the —backup option specifies the version control method. Alternatively, it can be specified by setting the VERSION_CONTROL environment variable. Here are the values to use for either one:
none, off | Never make backups (even if —backup is given). |
numbered, t | Make numbered backups. |
existing, nil | Numbered if numbered backups exist, simple otherwise. |
simple, never | Always make simple backups. |
If you use -b instead of —backup, the CONTROL method is always simple.
If you specify the -s option (which symlinks), ln ignores the -L and -P options. Otherwise (if you are making hard links), the last option specified controls behavior when a TARGET is a symbolic link. The default is to act as if -P was specified.
Examples
Create a hard link to the file public_html/myfile1.txt in the current directory.
Create a symbolic link to the file public_html/myfile1.txt in the current directory.
Create a symbolic link to the directory public_html named webstuff.
Creates a symbolic link to the file file1.txt named file2.txt. If file2.txt already exists, it is renamed to file2.txt
before the new file2.txt symlink is created.
Related commands
chmod — Change the permissions of files or directories.
link — Create a hard link to a regular file.
ls — List the contents of a directory or directories.
readlink — Print the value of a symbolic link or canonical file name.
unlink — Remove a file.
Источник