When we need to use the same file in different directories, we do not need to put a file that must be the same under each required Directory. We only need to put it in a fixed directory, put the file, and then use the ln command link in other directories to link it, without occupying disk space repeatedly.
Syntax
Ln [parameter] [source file or directory] [target file or directory] the parameter format is
[-BdfinsvF] [-S backup-suffix] [-V {numbered, existing, simple}]
[-- Help] [-- version] [--]
Command function:
In a Linux file system, there is a so-called link. We can regard it as the alias of the file, and there are two types of links: hard link) with symbolic link, a hard link means that a file can have multiple names, while a soft link generates a special file, the content of this file points to another file. Hard links exist in the same file system, but soft links can span different file systems.
No matter whether it is a hard link or soft link, it will not copy the original file, it will only occupy a very small amount of disk space.
Soft link:
- 1. Soft links exist as paths. Similar to shortcuts in Windows
- 2. Soft links can be cross-file systems, but hard links cannot.
- 3. Soft links can be used to link a non-existent file name
- 4. Soft links can be used to link directories
Hard link:
- 1. Hard links exist in the form of file copies. But it does not occupy the actual space.
- 2. Hard links cannot be created for directories.
- 3. Hard links can only be created in the same file system
Command parameters
Required parameters:
- -B: delete the file, overwrite the previously created link.
- -D allows the superuser to create hard links to directories.
- -F force execution
- -I interaction mode. If a file exists, the system prompts the user to overwrite the file.
- -N indicates the symbolic link as a general directory.
- -S soft link (symbolic link)
- -V: detailed handling process
Select parameters:
- -S "-S <suffix backup string>" or "-- suffix = <suffix backup string>"
- -V "-V <backup mode>" or "-- version-control = <backup mode>"
- -- Help: displays help information.
- -- Version: displays version information.
Instance
Create a soft link for the file and create a soft link link2013 for the log2013.log file. If log2013.log is lost, link2013 will become invalid:
Ln-s log2013.log link2013
Output:
[Root @ localhost test] # ll
-Rw-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 11 12-07 16:01 link2013-> log2013.log
-Rw-r -- 1 root bin
61 11-13 log2013.log
Create a hard link for the file and create a hard link ln2013 for log2013.log. log2013.log has the same attributes as ln2013.
Ln log2013.log ln2013
Output:
[Root @ localhost test] # ll
Lrwxrwxrwx 1 root 11 12-07 16:01 link2013-> log2013.log
-Rw-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 11 12-07 16:01 link2013-> log2013.log
-Rw-r -- 2 root bin 61 11-13 06:03 ln2013
-Rw-r -- 2 root bin 61 11-13 06:03 log2013.log