创建Hardlink
权限要求
- 硬链接的创建需要管理员权限
- 硬链接不可以盘(分区)创建
- 只有文件才可以创建硬链接,文件夹(目录)不可以
PS C:\Users\cxxu\Desktop> New-Item -ItemType HardLink -Path todirr -Target $env:desktop\recycleBin\
New-Item: A file is required for the operation. The item 'C:\Users\cxxu\desktop\recycleBin\' is not a file.
硬链接作用
可以修改一份文件,同时维护到其他关联文件
关于硬链接
- shell script - Determining if a file is a hard link or symbolic link? - Unix & Linux Stack Exchange
- 简单的说,就是电脑磁盘上的文件具有0个/1个或多个名字,这些名字就像路牌/指针,系统可以根据指向文件的指针找到文件所在位置(文件实体/文件数据),根据文件系统的组织方式,不同的目录(文件夹)下可以放置了一系列的文件指针,这些指针体现在用户面前就是一个个
文件
/目录文件夹
;
删除文件/目录
那么,当用户删除掉某个 文件
,这意味这什么?
- 实际上,用户删除掉的只是指向文件的一个指针,如果该指针所绑定的文件恰好就这么一个指针,那么你删除掉该指针,就再也不能找到该文件(在哪里了,效果相当于文件没有了(不可寻));
- 而一些数据恢复技术也是基于此特点实现的(帮助用户找回文件指针,文件就可用了)
- 而如果某个文件不知有一个指针(名字),那么当其中的一个指针被删除,我们仍然可以通过其他的名字找到该文件,也就是文件仍然可以正常别引用!
硬链接的所绑定的target是谁?
- 实际上,通过上述的分析,我们发现,文件的名字其实是平等的,没有谁绑定谁(在文件系统中)所有的路标都是直接指向文件实体,名字和名字之间并无关联,他们唯一的关联就是都直接指向同一个文件;
- 正因为此,我们可以借助已有的硬链接(某一个文件路径path)来创建同等地位的其他硬链接(同一个文件的新路径)(都指向同一个文件)(新的通往"罗马"的道路))
- 此外,使用powershell查找某个所谓的硬链接(事实上文件系统呈现给用户的文件一般都是硬链接!(处理一些被创建出来的软连接和符号链接)),linktype都是hardlink!
获取文件绝对路径
$absTarget = (Get-ChildItem $pattern).FullName
powershell 创建硬链接
通过
new-item -itemType hardlink
进行创建
注意,使用-target 相对路径无法成功创建,需要使用绝对路径
幸运的是,我们可以利用pwd来优化这一点
以下函数经过一定的处理,使得用户可以仅仅输入相对路径就可以创建符号连接
function newHardLink
{
param(
$Path = 'slides.md',
[String]$target
)
# 下面这段判断处理可有可无
<# if ($target.ToString().StartsWith(".\")) {
$target=$target.TrimStart(".\")
} #>
# $absTarget = "$pwd\" + "$target"
$absTarget = (Get-ChildItem $target).FullName
Write-Output "$absTarget will be target!"
# $absTarget = Invoke-Expression $absTarget
Write-Output "@$absTarget"
# $absTarget = $pwdPrefix + $target
Write-Output '@注意,Target必须使用绝对路径!'
Write-Output "@当然,也可以是这样的表达式:`"`$pwd\\file`""
Write-Output '@带上-target 选项'
if (Test-Path $Path)
{
rmFV $Path
}
New-Item -ItemType HardLink -Path $Path -Target $absTarget -Force -Verbose
}
- 经过初步测试,该函数能够兼容Target 参数为相对路径以及完整路径的情况
使用实例
PS D:\repos\scripts\ModulesByCxxu> sudo newHardLink -Path t1rofileAll.ps1 -target .\readme.md
D:\repos\scripts\ModulesByCxxu\readme.md will be target!
@D:\repos\scripts\ModulesByCxxu\readme.md
@注意,Target必须使用绝对路径!
@当然,也可以是这样的表达式:"$pwd\\file"
@带上-target 选项
VERBOSE: Performing the operation "Remove File" on target "D:\repos\scripts\ModulesByCxxu\t1rofileAll.ps1".
VERBOSE: Performing the operation "Create Hard Link" on target "Destination: D:\repos\scripts\ModulesByCxxu\t1rofileAll.ps1".
Directory: D:\repos\scripts\ModulesByCxxu
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 11/25/2021 7:13 PM 652 t1rofileAll.ps1
PS D:\repos\scripts\ModulesByCxxu> sudo newHardLink -Path t1rofileAll.ps1 -target $PROFILE.AllUsersAllHosts
D:\program files\powershell\7\profile.ps1 will be target! @D:\program files\powershell\7\profile.ps1 @注意,Target必须使用绝对路径!
@当然,也可以是这样的表达式:"$pwd\\file"
@带上-target 选项
VERBOSE: Performing the operation "Remove File" on target "D:\repos\scripts\ModulesByCxxu\t1rofileAll.ps1".
VERBOSE: Performing the operation "Create Hard Link" on target "Destination: D:\repos\scripts\ModulesByCxxu\t1rofileAll.ps1".
Directory: D:\repos\scripts\ModulesByCxxu
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 3/10/2022 11:10 AM 2920 t1rofileAll.ps1
linux shell 创建硬链接
-
man ln|nl|less
查看命令ln命令详情
2 NAME
3 ln - make links between files
4 SYNOPSIS
5 ln [OPTION]... [-T] TARGET LINK_NAME
6 ln [OPTION]... TARGET
7 ln [OPTION]... TARGET... DIRECTORY
8 ln [OPTION]... -t DIRECTORY TARGET...
9 DESCRIPTION
10 In the 1st form, create a link to TARGET with the name LINK_NAME. In the 2nd form, create
11 a link to TARGET in the current directory. In the 3rd and 4th forms, create links to each
12 TARGET in DIRECTORY. Create hard links by default, symbolic links with --symbolic. By de‐
13 fault, each destination (name of new link) should not already exist. When creating hard
14 links, each TARGET must exist. Symbolic links can hold arbitrary text; if later resolved,
15 a relative link is interpreted in relation to its parent directory.
- target是指,需要被创建链接的文件/目录名(变量)
- 在带
...
的用法中,可以同时指定多个target源(譬如用法3/4) - 用法三可以同时在一个目录中创建多个链接
创建示例
例1
创建指向python3.10(二进制文件)的符号链接symbolic link
# cxxu_kali @ CxxuWin11 in ~/dirToSymlinks [15:50:38] C:1
$ ln -s /usr/bin/python3.10 py3
# cxxu_kali @ CxxuWin11 in ~/dirToSymlinks [15:51:01]
$ ll
total 0
lrwxrwxrwx 1 cxxu_kali cxxu_kali 19 Apr 5 15:51 py3 -> /usr/bin/python3.10
示例2
利用符号链接给文件多加一个名字
# cxxu_kali @ CxxuWin11 in ~ [15:55:01] C:130
$ ln numbers num -s
检查链接
ls -l
该选项可以检查某个目录下的symbolic link
链接会以箭头指示
# cxxu @ cxxuAli in /usr/bin [15:34:47] C:130
$ ls -1l python*
lrwxrwxrwx 1 root root 9 Apr 16 2018 python -> python2.7
lrwxrwxrwx 1 root root 9 Apr 16 2018 python2 -> python2.7
-rwxr-xr-x 1 root root 3633000 Feb 27 2021 python2.7
lrwxrwxrwx 1 root root 9 Feb 8 14:51 python3 -> python3.6
-rwxr-xr-x 2 root root 4526456 Dec 9 05:08 python3.6
lrwxrwxrwx 1 root root 33 Dec 9 05:08 python3.6-config -> x86_64-linux-gnu-python3.6-config
-rwxr-xr-x 2 root root 4526456 Dec 9 05:08 python3.6m
lrwxrwxrwx 1 root root 34 Dec 9 05:08 python3.6m-config -> x86_64-linux-gnu-python3.6m-config
lrwxrwxrwx 1 root root 16 Oct 25 2018 python3-config -> python3.6-config
lrwxrwxrwx 1 root root 10 Feb 8 14:51 python3m -> python3.6m
lrwxrwxrwx 1 root root 17 Oct 25 2018 python3m-config -> python3.6m-config
file 命令
- 该命令可能需要手动安装
- 可以识别出
symbolic link
以及链接的target.
# cxxu @ cxxuAli in /usr/bin [15:37:52]
$ file python3
python3: symbolic link to python3.6
linux:硬连接和软连接的对比
referencs
- <<操作系统概念>>
- <<计算机操作系统(汤)>>
- 符号链接不具有被链接的文件的inode(index number)信息
- 符号链接仅仅包含被链接文件的
路径名
- 被链接的文件名(指针)被删除后,该符号链接就无法正确引用文件(尽管,文件本体可以被文件的其他指针(硬连接)找到(换句话说,尽管文件的引用计数count值哪怕不是0,符号链接都不能够确保能够正确引用文件
下面的实验中,注意
ls -li
第3个字段,每当一个inode(index number)增加一个硬连接,数字就会加1;删除,则数字减一;
而符号链接和文件的inode (index number)关系就比较松散,遂,发生了符号链接所指的文件名已经被删除了,但是符号链接依然还是指着那个不再存在的文件名(尽管文件名对应的文件还存在着(任然可以通过文件的另一个名字file2来访问)
#( 05/24/22@ 2:31PM )( cxxu@cxxuAli ):~/testLink
ec 'abc'>file1
#( 05/24/22@ 2:31PM )( cxxu@cxxuAli ):~/testLink
ls -li
total 4
1835411 -rw-rw-r-- 1 cxxu cxxu 4 May 24 14:31 file1
#( 05/24/22@ 2:31PM )( cxxu@cxxuAli ):~/testLink
ln file1 file2_hard
#( 05/24/22@ 2:32PM )( cxxu@cxxuAli ):~/testLink
ls -li
total 8
1835411 -rw-rw-r-- 2 cxxu cxxu 4 May 24 14:31 file1
1835411 -rw-rw-r-- 2 cxxu cxxu 4 May 24 14:31 file2_hard
#( 05/24/22@ 2:32PM )( cxxu@cxxuAli ):~/testLink
ln -s file1 file3_symbolic
#( 05/24/22@ 2:32PM )( cxxu@cxxuAli ):~/testLink
ls -li
total 8
1835411 -rw-rw-r-- 2 cxxu cxxu 4 May 24 14:31 file1
1835411 -rw-rw-r-- 2 cxxu cxxu 4 May 24 14:31 file2_hard
1835412 lrwxrwxrwx 1 root root 5 May 24 14:32 file3_symbolic -> file1
#( 05/24/22@ 2:32PM )( cxxu@cxxuAli ):~/testLink
rm file1
#( 05/24/22@ 2:32PM )( cxxu@cxxuAli ):~/testLink
ls -li
total 4
1835411 -rw-rw-r-- 1 cxxu cxxu 4 May 24 14:31 file2_hard
1835412 lrwxrwxrwx 1 root root 5 May 24 14:32 file3_symbolic -> file1
#( 05/24/22@ 2:32PM )( cxxu@cxxuAli ):~/testLink
bat file2_hard
───────┬───────────────────────────────────────────────────────────────────────────────────
│ File: file2_hard
│ Size: 4 B
───────┼───────────────────────────────────────────────────────────────────────────────────
1 │ abc
───────┴───────────────────────────────────────────────────────────────────────────────────
#( 05/24/22@ 2:32PM )( cxxu@cxxuAli ):~/testLink
bat file3_symbolic
[bat error]: 'file3_symbolic': No such file or directory (os error 2)
ls -li
#( 05/24/22@ 2:38PM )( cxxu@cxxuAli ):~/testLink
manly ls -l -i
ls - list directory contents
============================
-i, --inode
print the index number of each file
-l use a long listing format
ls -l
ls -l file1
-rw-rw-r--. 1 lilo lilo 0 Feb 26 07:08 file1
From the output above we can deduct a following information:
- -rw-rw-r- permissions
- 1 :number of linked hard-links
- lilo: owner of the file
- lilo: to which group this file belongs to
- 0: size
- Feb 26 07:08 modification/creation date and time
- file1: file/directory name