Linux ln command
The Linux ln command (short for "link files") is a very important command; its function is to create a synchronized link to a file at another location.
When we need to use the same file in different directories, we do not need to place an identical copy in every directory. We only need to put the file in one fixed directory and then use the ln command to link it in the other directories, without repeatedly taking up disk space.
Syntax
ln [参数][源文件或目录][目标文件或目录]
The format of the parameter is
[-bdfinsvF] [-S backup-suffix] [-V {numbered,existing,simple}]
[--help] [--version] [--]
Command function :
In the Linux file system, there are so-called links, which we can regard as aliases for a file. Links can be divided into two types: hard links and symbolic links. A hard link means a file can have multiple names, while a symbolic link creates a special file whose content points to the location of another file. Hard links must exist within the same file system, while symbolic links can span different file systems.
Neither hard links nor symbolic links copy the original file; they only take up a very small amount of disk space.
Symbolic link:
- 1. A symbolic link exists in the form of a path, similar to a shortcut in Windows operating systems
- 2. A symbolic link can span file systems, a hard link cannot
- 3. A symbolic link can link to a filename that does not exist
- 4. A symbolic link can link to a directory
Hard link:
- 1. A hard link exists in the form of a file copy, but does not occupy actual space.
- 2. Hard links cannot be created for directories
- 3. Hard links can only be created within the same file system
Command Parameters
Required Parameters:
- --backup[=CONTROL] back up an existing target file
- -b similar to --backup , but does not accept an argument
- -d allow the superuser to make hard links to directories
- -f force the operation
- -i interactive mode; prompt the user before overwriting an existing file
- -n treat a symbolic link as a normal directory
- -s symbolic link (soft link)
- -v display detailed processing
Select parameters:
- -S "-S
" or "--suffix= " - -V "-V
" or "--version-control= " - --help display help information
- --version display version information
Example
Create a symbolic link for a file: create a symbolic link link2013 for the file log2013.log. If log2013.log is lost, link2013 will become invalid:
ln -s log2013.log link2013
Output:
[root@localhost test]# ll
-rw-r--r-- 1 root bin 61 11-13 06:03 log2013.log
[root@localhost test]# ln -s log2013.log link2013
[root@localhost test]# ll
lrwxrwxrwx 1 root root 11 12-07 16:01 link2013 -> log2013.log
-rw-r--r-- 1 root bin 61 11-13 06:03 log2013.log
Create a hard link for a file: create a hard link ln2013 for log2013.log; log2013.log and ln2013 have identical attributes
ln log2013.log ln2013
Output:
[root@localhost test]# ll
lrwxrwxrwx 1 root root 11 12-07 16:01 link2013 -> log2013.log
-rw-r--r-- 1 root bin 61 11-13 06:03 log2013.log
[root@localhost test]# ln log2013.log ln2013
[root@localhost test]# ll
lrwxrwxrwx 1 root root 11 12-07 16:01 link2013 -> log2013.log
-rw-r--r-- 2 root bin 61 11-13 06:03 ln2013
-rw-r--r-- 2 root bin 61 11-13 06:03 log2013.log
Linux Command Reference