Links are one of the widely used features in Linux. In its most basic sense, Links are just special files that allow you to create references to other files and directories. It has many advantages and it works more or less like a regular file or directory. You can access and operate on a link just as you can perform different functions on a file.
There are essentially two different types of links in Linux: Hard Links and Soft Links. Most times the copy process itself does not have to differentiate between the two types of links. However when you talk about copying a link, it could mean one of three things:
Copy Content or Referenced File: This involves copying the contents of file that the link points to. The contents of the referenced file is copied on to another regular file.
Link to Reference: This will copy the link itself to another link and the new link will also point to the file that the original link pointed to. This is same as creating a new link to the referenced file.
Symbolic Link to Link: Another variation of the copy process is to create a link to the link itself, but that falls more in line with how to create a symbolic link than the copy process.
As you can see all of three variations effectively creates a new file which is either a copy or reference of the original or source file. As with copying regular files and directories, you can use the cp command to copy symbolic links as well. The cp command has several command line options that helps to deal with the symbolic links.
We will try to follow the same file names as examples throughout the post. Let’s assume that the regular file with content is named sourceone. There exist a symbolic link named linkone that points to the file sourceone. We will now copy the link named linkone to a target file named targetone. All these files exist in the same folder.
Copy Reference File
-L or –dereference
The -L option specifies that the link should be followed if it is one. This means that the referenced file itself will be copied to the new location rather than the link. This is the default option, which means that a default copy of symbolic link using the cp command will copy the file contents of the referenced file instead of the link itself.
Both of the following examples perform the same operation, it copies the referenced file to the new location. The file targetone will be a copy of the file sourceone (which linkone points to) as a result of the commands shown below.
bash$ cp linkone targetone
bash$ cp -L linkone targetone
When used recursively with the -r option, the final copied location will be devoid of all symbolic links but it will contain all files and links with the content. The symbolic links would have now been converted to regular files with copied content.
Link to Reference File
-P or –no-dereference
The -P or –no-dereference option specifies never to follow the symlink or symbolic links in the source. When you use this option with the cp command, it will create a symbolic link to the source file or the referenced file.
bash$ cp -P linkone targetone
When used with -r option to copy folders, this will produce an exact replica of the source folder with the files and symbolic links pointing to the newly copied source files.
Another interesting option is –preserve which allows you preserve various attributes of the source file when copying. One of the attribute that you can specify is the links. This will preserve the link feature or attribute of the file, which the works the same way as creating a new link to the referenced file.
There are couple options which are more convenient to use than having to type the long command line options. The -d option is a shortcut to –no-dereference –preserve=links. Thus using the -d option will allow you to create a link to the referenced file just as the -P option.
bash$ cp -d linkone targetone
Another commonly used option is -a or –archive which is the same as -dpR. In the long form, it is –no-dereference –preserve=link,mode,ownership,timestamps –recursive. This can be used to backup or copy files and directories recursively while still preserving the links and file attributes.
Symbolic Link to Link
-s or –symbolic-link
The -s or –symbolic-link command line option will create a symbolic link to the copied file. This is much more of an useful option when copying regular files. If you use this option when copying symbolic links it will create a link to the link itself.
bash$ cp -s linkone targetone
In the above example, targetone is now a symbolic link to the link linkone (and not directly to file sourceone). The -r option does not always play well with the -s option and its success depends on the type of links you have. This is because the -s option can only make relative symbolic links only in the current working directory.
The option to copy directories and folders recursively is -r, -R or –recursive. You can use this option with most of the command line options mentioned above to copy files, folders and links recursively.