The function of ln is to create a synchronous link for a file in another location. When using the same file in different directories, we don't need to put the same file in each directory. We just need to put the file in a fixed directory, and then link it with ln command in other directories. We don't need to occupy disk space repeatedly.
1. Command format
ln [parameter] [source file or directory] [target file or directory]
2. Command Function
We can consider links as aliases for files, and links can be divided into two kinds: hard links and symbolic links. Hard links mean that a file can have multiple names, while soft links generate a special file and point to the location of another file; hard links can exist in the same file system, while soft links can span different file systems.
Softlink
Soft links exist in the form of paths, similar to shortcuts in Windows systems
Soft links can span multiple file systems, but hard links can't.
Soft links can link to a non-existent filename
Soft links can link directories
Hard Link
Hard links exist as copies of files, but do not take up real space.
Do not create hard links to directories
Hard links can only be created in the same file system
Here are two points to note:
First, the ln command keeps the synchronization of every linked file, that is to say, no matter where you change it, the other files will change the same.
Secondly, the links of LN are divided into soft links and hard links. Soft links are Ln-S source file object files. It only generates a mirror of the file in the location you choose. It does not occupy disk space. Hard links to ln source file object files, without parameters-s, will generate a sum source in the location you choose. Files of the same size, whether soft links or hard links, keep the file changing synchronously.
The ln directive is used to link files or directories. If more than two files or directories are specified at the same time and the final destination is an existing directory, all the files or directories specified earlier will be copied to that directory. If multiple files or directories are specified at the same time and the final destination is not an existing directory, an error message will appear.
3. Command parameters
3.1 Necessary parameters
- b. Delete or overwrite previously established links
- d. Allow superusers to make hard links to directories
- f. Enforcement
- i, interactive mode, prompting users whether to overwrite existing files
- n, treat symbolic links as general directories
- s, Soft Link (Symbolic Link)
- v, showing the detailed process
3.2 Selection of parameters
- S, "-S < suffix backup string >" or "- suffix = < suffix backup string >"
- V, "-V < Backup Mode >" or "- version-control= < Backup Mode >"
- help, display help information
Version, showing version information
4. Use examples
Example 1: Create soft links for files
Command: Ln-S log2013. log link 2013
Description: Create a soft link link2013 for the log2013.log file. If the log2013.log is lost, link2013 will be invalid.
# ll
-rw-r--r-- 1 root bin 61 11-13 06:03 log2013.log
# ln -s log2013.log link2013
# 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
Example 2: Create hard links for files
Command: ln log2013.log ln2013
Note: Create hard link ln2013 for log2013.log. The attributes of log2013.log and ln2013 are the same.
# 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
# ln log2013.log ln2013
# 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
# cat ln2013
hostnamebaidu=baidu.com
hostnamesina=sina.com
hostnames=true
Example 3: Delete and rebuild the original file after linking
# 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
# rm -rf log2013.log
# ll
lrwxrwxrwx 1 root root 11 12-07 16:01 link2013 -> log2013.log
-rw-r--r-- 1 root bin 61 11-13 06:03 ln2013
# touch log2013.log
# ll
lrwxrwxrwx 1 root root 11 12-07 16:01 link2013 -> log2013.log
-rw-r--r-- 1 root bin 61 11-13 06:03 ln2013
-rw-r--r-- 1 root root 0 12-07 16:19 log2013.log
# less > log2013.log<<EOF
>2013-01
>2013-02
>2013-03
>2013-04
>2013-05
>2013-06
>2013-07
>2013-08
>2013-09
>2013-10
>2013-11
>2013-12
>EOF
# ll
lrwxrwxrwx 1 root root 11 12-07 16:01 link2013 -> log2013.log
-rw-r--r-- 1 root bin 61 11-13 06:03 ln2013
-rw-r--r-- 1 root root 96 12-07 16:21 log2013.log
# cat link2013
2013-01
2013-02
2013-03
2013-04
2013-05
2013-06
2013-07
2013-08
2013-09
2013-10
2013-11
2013-12
# cat ln2013
hostnamebaidu=baidu.com
hostnamesina=sina.com
hostnames=true
Explain:
After deleting the source file, the hard link is not affected; the soft link file flickers continuously under centos system, indicating that the source file does not exist.
After rebuilding the source file, the soft links no longer flicker, indicating that the soft links have been successful and the linked file system has been found; after rebuilding, the hard links are not affected by the source file, and the contents of the hard links still retain the contents of the source file before deletion, indicating that the hard links have failed.
Example 4: Link files to the same name in another directory
Command: ln log2013. log. / test3/
# ln log2013.log ./test3/
# cd ./test3/
# ll
-rw-r--r-- 2 root root 96 12-07 16:21 log2013.log
# less > log2013.log <<EOF
>2013-01
>2013-02
>2013-03
>2013-04
>2013-05
>2013-06
>2013-07
>2013-08
>2013-09
>2013-10
>EOF
# ll
-rw-r--r-- 2 root root 80 12-07 16:36 log2013.log
# cd ..
# ll
lrwxrwxrwx 1 root root 11 12-07 16:01 link2013 -> log2013.log
-rw-r--r-- 1 root bin 61 11-13 06:03 ln2013
-rw-r--r-- 2 root root 80 12-07 16:36 log2013.log
# more log2013.log
2013-01
2013-02
2013-03
2013-04
2013-05
2013-06
2013-07
2013-08
2013-09
2013-10
Description: Create the same name link of log2013.log in the test3 directory, modify the log2013.log file in the test3 directory, and synchronize to the file in the source directory.
Example 5: Create a soft link to the directory
Command: ln-sv/opt/soft/test/test3/opt/soft/test/test5
# ll
drwxr-xr-x 2 root root 4096 12-07 16:36 test3
drwxr-xr-x 2 root root 4096 12-07 16:57 test5
## ln -sv test3 test5
//Tes3 is the source directory and test5 is the link directory
# cd test5
# ll
lrwxrwxrwx 1 root root 5 12-07 16:57 test3 -> test3
# cd test3
-bash: cd: test3: Too many layers of symbolic connection
# rm -rf test3
# ll
# ln -sv /opt/soft/test/test3 /opt/soft/test/test5
//Create a symbolic link to "/opt/soft/test/test3" "/opt/soft/test/test5/test3"
# ll
lrwxrwxrwx 1 root root 20 12-07 16:59 test3 -> /opt/soft/test/test3
# cd test3
# ll
//Total 4
-rw-r--r-- 2 root root 80 12-07 16:36 log2013.log
# touch log2014.log
# ll
//Total 4
-rw-r--r-- 2 root root 80 12-07 16:36 log2013.log
-rw-r--r-- 1 root root 0 12-07 17:05 log2014.log
Explain:
Only soft links to directories can be created.
Soft links to directories must be created using absolute paths. Relative paths will not be successful. Tip: too many layers of symbolic links.
Modifying files in the linked directory synchronizes changes in the source directory.