Published on

ln command-create a link to a file



3 min read
The ln command is a very important command in the linux system. The full English name is "link", which means link. Its function is to establish a synchronized link for a file in another location. One is hard link, also known as hard link; the other is symbolic link, also known as symbolic link. In a simple way, you can think of a hard link as a copy of the source file. It is the same size as the source file, but in fact it does not occupy any space. Symbolic links can be understood as shortcuts similar to windows.

Symbolic link:

1. Symbolic links exist in the form of paths, similar to shortcuts in the Windows operating system. 2. Symbolic links can cross file systems, but hard links cannot. 3. Symbolic link can link a non-existent file name, hard link cannot. 4. Symbolic links can link directories, but hard links cannot.

Hard link:

1. Hard links exist in the form of file copies, but do not occupy actual space.2. Hard links are not allowed to create hard links to directories.3. Hard links can only be created in the same file system.


ln [parameter] [source file or directory] [target file or directory]

Command parameters

-b Create a backup file for each existing target file
-d This option allows "root" users to establish hard links to directories
-f Force link creation even if the target file already exists
-n Treat a symbolic link pointing to a directory as an ordinary file
-i Interactive mode, if the target file already exists, the user is prompted to confirm to overwrite
-s Create symbolic links to source files, not hard links
-v Detailed information mode, detailed execution process of output instructions


Create a hard link file_1 for the source file file.txt:
[root@linuxstar ~ ]# ln /root/dir/file.txt ./file_1
Use the "-s" parameter of the ln command to create a symbolic link to the directory, and use the ls command to view the detailed information of the linked file:
[root@linuxstar ~]# ln -s dir file
[root@linuxstar ~]# ls -l 
total usage 4-rw-------. 1 root root 1138 March 11 14:48 anaconda-ks.cfgdrwxr -xr-x. 2 root root 36 April 3 08:47 testlrwxrwxrwx. 1 root root 4 April 3 08:54 file -> dir
Use the "-v" parameter of the ln command to output the detailed execution process of the command:
[root@linuxstar ~]# ln -v /root/dir/file.txt ./file_1 './file_1' => '/root/dir/file.txt'
Use the "-b" command of the ln command to create a backup file of the target file, and use the ls command to view:
[root@linuxstar ~]# ln -b /root/dir/file.txt ./file_1
[root@linuxstar ~]# ls anaconda-ks.cfg  file_1  file_1~  dir