A symlink (a symbolic link or soft link) is a file containing a reference to another file or directory in Linux. They create shortcuts to other files and folders and are particularly helpful in locating ones with longer paths. While a symlink contains a pointer to the original file or folder, even those located on other file systems, a
Where a symlink is like a pointer to a file or directory, a hard link is a copy of it and can access the data contained within the original file or directory. If we delete the original file/directory, the hard link persists and will still contain the original data. However, a symlink behaves differently; it can’t directly access the file or directory data, will point to a nonexistent file or directory object if they are deleted, and will not work like before. Files and hard-linked directories will have the same inode number; this is not the case with symlinks. Also, symlinks are slower than hard links. Lastly, hard links can not be created for directories, while we can create symlinks for both files and directories.
We’ll create four folders, one inside the other, starting from folder1
and ending with folder4
, similar to those shown in the diagram below. Observe how long the path to the last folder is.
Next, we create a file called my_original_file.txt
in folder4
, the last nested folder. We create the actual symlink with the ln
command. The -s
command specifies that a soft link has to be made. In our case, the original file path will look like /folder1/folder2/folder3/folder4/my_original_file.txt
. For the symlink_path
, give a shorter path, like creating a normal file inside a folder located at a higher level than the nested ones in the folder tree.
ln -s original_file_path symlink_path
We’ll use the ls
command to see whether the symlink has been successfully created inside the symlink_path
specified. Next, to truly bear the fruits of our efforts, we’ll paste a simple string inside the original file using the symlink’s path and not the original file path using echo "hello, catch me if you can!" > /home/my_symlink_file
, and check if it has indeed been pasted inside the original one.
mkdir /folder1 && mkdir /folder1/folder2 && mkdir /folder1/folder2/folder3 && mkdir /folder1/folder2/folder3/folder4touch /folder1/folder2/folder3/folder4/my_original_file.txt &&\ls /folder1/folder2/folder3/folder4 &&\ln -s /folder1/folder2/folder3/folder4/my_original_file.txt /home/my_symlink_file &&\cd /home &&\ls -l &&\echo "hello, catch me if you can!" > /home/my_symlink_file &&\cd /folder1/folder2/folder3/folder4 &&\cat /folder1/folder2/folder3/folder4/my_original_file.txt
Now that we’re done with all the technical details Run the terminal below as working proof.
Creating a symlink for a directory is similar to that for a file. We’ll use the same folder hierarchy as above, and to check whether our efforts paid off, we create a new file my_file.txt
, and try to paste it within the nested directory folder4
using the symlink’s path.
mkdir /folder1 && mkdir /folder1/folder2 && mkdir /folder1/folder2/folder3 && mkdir /folder1/folder2/folder3/folder4ln -s /folder1/folder2/folder3/folder4 /home/my_symlink_dir &&\cd /home &&\ls -l &&\touch my_file.txt &&\ls &&\cp my_file.txt /home/my_symlink_dir &&\cd /folder1/folder2/folder3/folder4 && ls
So, did we succeed? Check it out for yourself!
To summarize, symlinks are like shortcuts to files and directories and can be used to manipulate their data.
Further readings include:
Free Resources