1. Overview
In this tutorial, we’ll go over various methods for converting symlinks into regular files. This can be useful when we want to remove the original source from which we created the symlink.
2. What Are Symlinks?
Symlinks in Linux allow us to link a single file or directory to multiple locations. In other words, they will enable us to access a single file or directory from various places where we have created symlinks for it.
Let’s take Linux distributions as an example. Many distributions symlink the directories /usr/bin and /usr/lib to /bin and /lib, respectively, allowing programs to access files in /usr/bin using paths starting with /bin.
We use the ln command to create symlinks:
$ echo "Hello" > original_file
$ ln -s original_file link_to_file
$ ls -l
total 4
-rw-r--r-- 1 baeldung baeldung 6 Jan 21 16:29 original_file
lrwxrwxrwx 1 baeldung baeldung 13 Jan 21 16:29 link_to_file -> original_file
$ sha256sum original_file
66a045b452102c59d840ec097d59d9467e13a3f34f6494e539ffd32c1bb35f18 original_file
$ sha256sum link_to_file # Points to 'original_file'
66a045b452102c59d840ec097d59d9467e13a3f34f6494e539ffd32c1bb35f18 link_to_file
The symlink is indicated with the -> symbol in the ls command output.
3. Converting a File Symlink to File
We can use various methods for converting symlinks into regular files. The main idea is to remove the symlink and copy the original file to the same destination.
3.1. The realpath and cp Commands
We can use the realpath command to get the absolute path to the original file. Then, we can use this path with cp with the -f flag to copy it over and overwrite the symlink:
$ ls -l
total 4
-rw-r--r-- 1 baeldung baeldung 6 Jan 21 16:45 original_file
lrwxrwxrwx 1 baeldung baeldung 13 Jan 21 16:39 link_to_file -> original_file
$ cp -f "$(realpath link_to_file)" link_to_file
$ ls -l
total 8
-rw-r--r-- 1 baeldung baeldung 6 Jan 21 16:45 original_file
-rw-r--r-- 1 baeldung baeldung 6 Jan 21 16:46 link_to_file
$ cat link_to_file
Hello
As we can see, link_to_file has been converted to a regular file, and ls does not show the -> symbol anymore. cp also preserves the original permissions of the file.
3.2. The sed Command
The sed command is usually used for editing text files, but we can also use it for copying files:
$ ls -l
total 4
-rw-r--r-- 1 baeldung baeldung 6 Jan 21 16:45 original_file
lrwxrwxrwx 1 baeldung baeldung 13 Jan 21 17:09 link_to_file -> original_file
$ sed -i ';' link_to_file
$ ls -l
total 8
-rw-r--r-- 1 baeldung baeldung 6 Jan 21 16:45 original_file
-rw-r--r-- 1 baeldung baeldung 6 Jan 21 17:10 link_to_file
The -i flag makes sed rewrite the file in-place, whereas the semi-colon stands for performing no operations.
We should note that sed only copies the original contents of the file, but not the permissions or ownership.
4. Converting A Directory Symlink To Directory
We can also create symlinks to directories, but the process of converting directory symlinks into regular directories is a bit different than files.
For this, we can use the rm command to remove the symlink and the cp command to copy the original directory:
$ ls dir/
original_file
$ ls -l
total 0
drwxr-xr-x 2 baeldung baeldung 60 Jan 21 17:16 dir
lrwxrwxrwx 1 baeldung baeldung 3 Jan 21 17:15 link_to_dir -> dir
$ real_dir="$(realpath link_to_dir)" # Store original path
$ rm link_to_dir
$ cp -LR "$real_dir" link_to_dir
$ ls -l
total 0
drwxr-xr-x 2 baeldung baeldung 60 Jan 21 17:16 dir
drwxr-xr-x 2 baeldung baeldung 60 Jan 21 17:19 link_to_dir
$ ls link_to_dir/
original_file
Here, link_to_dir was a symlink to the dir directory. We first store the path to the original directory in a variable, then remove the symlink and copy over the original directory in its place. Finally, we pass the -L and -R flags to cp to ensure that all symlinks are resolved, and contents are copied recursively.
5. Conclusion
In this article, we learned about using various commands like realpath, cp, and sed for converting symlinks into regular files or directories.