link files for linux Use the ln command to create a linked file (link)
There are two types of Linux links: Hard links, Symbolic links (symbolic link)
By default, theln command produces a hard link .
[email protected] lianxi]# ln-s xiaotong tongtong #符号链接才能为目录建立链接source file destination file (linked file)
[Email protected] lianxi]# CD tongtong/
[Email protected] tongtong]# pwd
/lianxi/tongtong
[[email protected] tongtong]# Pwd-p show the true path of the Tongtong directory
/lianxi/xiaotong
1. Hard link
command format:Ln Source File ... Link file
A hard connection is a connection made through an index node : an inode number that corresponds to multiple file names ( a file has multiple different file names )
Role:
Allows a file to have more than one valid path name, to establish a hard connection to prevent "accidental deletion " of the function.
# # # #文件真正删除的条件是与之相关的所有硬连接文件均被删除.
2 Restrictions on hard connections:
① points to the data storage location for the original file and cannot establish a hard-link file for the directory ② Hard links must be in the same partition (file system) as the original file
2, Soft link (symbolic Link) Symbolic Link: command format:LN- s source file ... Link file-S,--symbolic make symbolic links instead of hard linksone file linked to another file ( similar to a shortcut to Windows ) Soft Point to the path where the original file is located (soft link source file with absolute path, but better with relative path)
Principle:
A soft connection file is actually a special text file that contains location information for another file.
Allow Symbolic links (often abbreviated as symlinks) to point to a file that is located on another partition or even on another network hard disk.
[[email protected]lianxi]# Touch Test #创建一个测试文件Test
[[email protected] lianxi]# ln test h_test A hard connection file for #创建 test h_test
[[email protected] lianxi]# ln-s test s_test A symbolic connection file for #创建 test s_test
[[email protected] lianxi]# Ls-li #-I parameter displays inode node information for a file
Total 0262146-rw-r--r--2 root root 0 Feb 23:31 test262146-rw-r--r--2 root root 0 Feb 23:31 h_test262196 lrwxrwxrwx 1 root root 4 Feb 23:31 s_test -Test
Analysis: The hard connection file h_test the same as the inode node of the original file test , all 262146, however, the inode node of the symbolic connection file is different.
[Email protected] lianxi]# echo "I am Test File" >>test[Email protected] lianxi]# cat testI am Test file[Email protected] lianxi]# cat h_testI am Test file[Email protected] lianxi]# cat s_testI am Test file[Email protected] lianxi]# rm-f test[Email protected] lianxi]# cat h_testI am Test file[Email protected] lianxi]# cat s_testcat:S_test:No such file or directory
Analysis: Hard connect h_test is not affected when the original file test is deleted, but the symbolic connection s_test file is not valid
Summarize:
① Delete the original file, the hard connection file has no effect, resulting in the symbolic connection file (dependency and source file) invalid;
② Delete the symbolic connection file, the source files, hard-linked files have no effect;
③ Delete Hard connection file, the source file, symbol connection file also has no effect;
④ Delete both the original file and the hard connection file, the entire file will be really deleted. establish a hard-link file to play a role in backup.
##### Modify any file content, the other linked files will be modified accordingly
Linux link File-ln command