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.


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).


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 . ↵
$ ln file file2 ↵ Create a hard link
$ ls -il file* ↵
559793 -rwxr-xr-x 2 root root 12428 May 31 2007 file Copied file
559793 -rwxr-xr-x 2 root root 12428 May 31 2007 file2 Hard linked file
$ rm file ↵ Delete original file
$ ./file2 ↵ Execute hard linked file
Usage: file2 [-bcikLnNsvz] [-f namefile] [-F separator] [-m magicfiles] file...
file2 -C -m magicfiles You could execute hard linked file
Try `file --help' for more information.
# rm file2 ↵ Delete hard linked 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.
$ ln -s file file3 ↵ Create symbolic link file(file3) of the file
$ ls -il file* ↵ List files which have string 'file' at the beginning of the file names
559793 -rwxr-xr-x 2 root root 12428 May 31 2007 file Copied file
360502 lrwxrwxrwx 1 root root 4 Feb 9 04:55 file3 -> file-rwxr-xr-x 1 Symbolic link file
$ rm file ↵ Delete the original file of symbolic link file(file3)
$ ./file3 ↵ Execute symbolic link file
-bash: ./file3: No such file or directory Error occurs because there is no linked source




Previous Next