Linux+: Linux Shell 22 – File Linking – Soft Links


Jarret W. Buse

Linux+: Linux Shell 22 – File Linking – Soft Links

Files are stored on a partition and within a directory structure. Files can be stored by two link methods which are file links. For some people, file links can be a difficult subject. With a little patience they can be understood.

The two linking methods are:
  1. Hard Link
  2. Soft Link (Symlink)
Files are stored on a partition in two places. The first place is the inode table which stores the filename, various times (modify, access, creation, etc.), owner, permissions, inode address, etc. The inode address is the address of the physical file location in the partition.

To begin with, partitions contain a section for the inode table as well as a pool of free inodes. When a file or folder is placed into the partition it requires a free inode address. The file information is placed into the inode table with a free inode address. The file is then placed at the inode address.

A soft link is when a file exists on a partition and has another file which points to the first file. The soft link will not have the same inode address as the original file. The soft link, or Symlink, is a file which contains a pointer to the original file. The pointer is the inode address. When a file accesses a Symlink, the pointer is read and the link is followed to the actual file.

To make a soft link, use the command 'ln' with the -s option. The syntax is as follows:

ln -s source_file target_file

Looking at Figure 1, there is a file named 'name'. The 'ln -lia' command shows that the file has an inode address of 949199. A soft link is then created which is located at inode address 949200. The original file has a size of 12 bytes and the soft link is 4 bytes. The permissions are different for the two files.

Figure 22-1.jpg


When looking at the permissions for the soft link, you will notice the 'l' at the beginning. The 'l' denotes that the file or folder is a link, specifically a soft link.

To see the contents of a symlink, use the 'readlink' command. The syntax is as follows:

readlink filename

The contents of the file 'text' is shown in Figure 2. The value is the name of the file being pointed to, and in this case has no path since it is in the same folder.

Figure 22-2.jpg


It should be noted that hard links were required to be on the same partition. Soft links can span partitions and even drives.

An example of this is shown in Figure 3. Here we use the file 'name' and make a soft link to another partition called '/media/jarret/Bohdi/home/jarret/Test/'. Notice how the source path in the ln command is an absolute path. Be sure to use absolute paths when possible. This information is what is placed in the soft link pointer as shown in Figure 3.

Figure 22-3.jpg


If the soft link file remains in the local folder, then the file name is enough. Once the path is out of the local folder, absolute paths are needed.

If the source file is deleted then the soft link is considered 'dangling' or 'unattached'. A dangling soft link means that the pointer within the link is no longer valid. If the soft link is clicked within the Graphical User Interface (GUI), an error will be generated that the link is broken.

To some, soft links are thought of as shortcuts similar to those used in Windows systems. In a basic regard, this is true. Window shortcuts allow for properties to be set for executables when they are clicked.

Soft links work with directories where hard links do not. Again, the soft links can cross over partition boundaries and even Hard disk Drive (HDD) boundaries. To copy a directory as a soft link, use the following syntax:

ln -sd source_folder target_folder

The process is similar to creating a soft link with a file. If the source folder is deleted, then the soft link is dangling and will not work when clicked.

When any changes are made to the source file or folder, the changes are only made to that file or folder. The soft link is only a pointer to that file or folder which will open the source of the pointer and the changes are seen. To delete the soft link itself will have no effect on the source file or folder.

Since the contents of the soft link are the absolute path to the source, if the source or any folder name of the path is renamed, the soft link is broken. Renaming the folder or path folder back to the original name will cause the soft link to function again. Whether the source is a file or folder, the soft link will fail if the source is moved as well.

Keep in mind that the soft link is an absolute path to the source and it must still exist as it is listed within the soft link.

Soft links may require a little practice to create, but they can be simple and handy to have as shortcuts to specific files and folders.

$100 Digital Ocean Credit
Get a free VM to test out Linux!

Members online