10.9 Hard Link and Symbolic Link |
The link function is a function that can be treated as if it is in a different directory without copying or moving the file.
For example, if you specify a file or directory as an argument of a command, you may mistake the input if the path specification is long and complicated. Using the link function, you can treat frequently used files as if they were placed in your home directory.
There are two types of links: hard links and symbolic links.
- Hard link
The hard link points directly to the entity of the file and shares it. Deleting a hard link does not delete the original file. Since hard links are realized by sharing inode numbers, they can not be created in separate file systems (separate partitions).
- Symbolic link
The symbolic link creates a pseudo file indicating the location (path) where the source file is stored. Erasing the symbolic link does not affect the original file. Erasing the original file causes an error from the symbolic link. Symbolic links can be created between different file systems. On Windows, the concept is the same as a file called shortcut.
Format |
Create a hard link
ln Original_file_name Link_name |
Create a hard link.
Create a symbolic link.
Options |
-s |
Create a symbolic link.
Practice : Create a hard link |
Copy the file command and create file2 which is hard link of the file with the ln command. Even if file file is deleted, file 2 can be executed, so you can see that the data is not deleted.
$ cp -p /usr/bin/file . ↵ |
The file is always linked to the inode information, so creating a file is the same as you can have one hard link. If you make a file hard link, the number of links in the inode area increases by one, and deleting the hard link decreases the number of links by one. When the file is deleted and the number of links becomes 0, the file data is completely deleted from the file system.
Practice : Create a symbolic link |
Copy the file command in the current directory and create file3 as a symbolic link for the file command. To create a symbolic link, add the -s option to the ln command. If file3 is executed after deleting file, it will result in an error stating that there is no file of the symbolic link source. To execute a file in the current directory, "./" must be specified before the file. When examined by the ls command, the symbolic link file has character 'l' of the head of permission information.
$ cp -p /usr/bin/file . ↵ Copy the file command to current directory. |