Windows中文件和文件夹的链接类型(快捷方式、软链接、硬链接、符号链接)

链接四大类型

There are three types of file links supported in the NTFS file system: hard links, junctions, and symbolic links.

  • 快捷方式
  • 软链接
  • 硬链接
  • 符号链接

快捷方式(Shortcut)

A Shell link is a data object that contains information used to access another object in the Shell’s namespace—that is, any object visible through Windows Explorer. The types of objects that can be accessed through Shell links include files, folders, disk drives, and printers

在Windows上以*.lnk结尾的文件,这类文件通常用于指定某一个文件或某一个目录的位置,可扩展性很强,桌面快捷方式就是这类了。

软链接/硬链接(Junction[Soft link] / Hard link)

一个口诀,软链目录,硬链文件。也就是说,软链接只能链接到目录,硬链接只能链接到文件。

A hard link is the file system representation of a file by which more than one path references a single file in the same volume

A junction (also called a soft link) differs from a hard link in that the storage objects it references are separate directories

硬链接的文件属性是同步的,A <==> B,A文件是B文件的硬链接,那么改动A的属性(比如说隐藏A)会同步改动到B的属性,改动B的属性会同步到A。

符号链接(Symbolic link)

A symbolic link is a file-system object that points to another file system object. The object being pointed to is called the target.
Symbolic links are transparent to users; the links appear as normal files or directories, and can be acted upon by the user or application in exactly the same manner.

符号链接比较强大,可链接文件和文件夹。符号符号,听这个意思就是可以”乱指“。指哪都行。

It is possible (but not advisable) to create two links that point to each other in a loop, or a link that targets itself. Symbolic links can expose security vulnerabilities in applications that aren’t designed to handle them.

区别

类型\属性 能否链接到文件 能否链接到目录 能否跨越磁盘分区 能否指向不存在的目标 能否指向相对目录 如何删除
快捷方式 del file
硬链接 del file
软链接 rd folder
符号链接 rd folder or del file

命令行创建链接

首先,快捷方式是不能从命令行进行创建的,创建方式只能通过手动右键创建和调用COM接口(IShellLink)创建,不可通过脚本命令来创建。这就对编程带来了一些困难,因为要调用COM接口,不是那么直接。

当然,网上有编译好的exe可以创建快捷方式http://www.optimumx.com/downloads.html#Shortcut,这个里面也是利用COM接口然后使用C++编写的。

而软链接、硬链接、符号链接是可以通过命令mklink来创建的。

Syntax
  MKLINK [[/D] | [/H] | [/J]] LinkName Target
Key:
  /D    Create a Directory symbolic link. (default is file)
  /H    Create a hard link instead of a symbolic link.
  /J    Create a Directory Junction.
  LinkName The new symbolic link name.
  Target The path (relative or absolute) that the new link refers to.

使用mklink需要管理员权限,且不能覆盖,如果存在同名链接,只能删除然后再创建

API创建链接

这里推荐下这篇文章,这里面揽括了硬链接、符号链接的Windows API创建目录软链接的黑魔法,以及快捷方式的COM用法
http://www.flexhex.com/docs/articles/hard-links.phtml

示例

执行下列脚本,生成链接。

@echo off

set root_path=%userprofile%\link
set app_path=%root_path%\app_1.1.5.235
set readme_path=%app_path%\readme.txt

:: 创建根目录
mkdir %root_path%

:: 创建程序目录
mkdir %app_path%

:: 创建程序目录软链接
mklink /j %root_path%\app_junction %root_path%\..\link\app_1.1.5.235

:: 创建程序目录符号链接
mklink /d %root_path%\app_symbolic %root_path%\..\link\app_1.1.5.235

:: 手动创建程序目录快捷方式 %root_path%\app_shortcut.lnk

:: 创建文件 readme.txt
echo generate link >> %readme_path%

:: 创建文件硬链接
mklink /h %app_path%\readme_hard.txt %readme_path%

:: 创建文件符号链接
mklink %app_path%\readme_symbolic.txt %readme_path%

:: 手动创建文件快捷方式readme.lnk

可以看到如下结果:

在这里插入图片描述

这里可以看到,在使用mklink时创建链接时,目标路径我都用了相对路径,但是可以看到软链接的目标路径已经计算成了绝对路径,而符号链接的目标路径还是之前写进去的相对路径,同样地,快捷方式和硬链接的目标路径也是绝对路径。

参考

发布了299 篇原创文章 · 获赞 352 · 访问量 45万+

猜你喜欢

转载自blog.csdn.net/FlushHip/article/details/104685928