【Linux】【Windows】目录链接
Linux系统
命令:ln -s 目标目录 链接目录
ln -s /data/ycx /ycx
ll 显示 ycx -> /data/ycx
Windows系统,必须管理员权限运行命令
cms
命令:mklink /D 链接目录 目标目录
mklink /D D:\ycx D:\data\ycx
为 D:\ycx <<===>> D:\data\ycx 创建的符号链接
dir 显示 <SYMLINKD> ycx [D:\data\ycx]
PowerShell
命令:New-Item -ItemType SymbolicLink -Path 链接目录 -Target 目标目录
New-Item -ItemType SymbolicLink -Path D:\ycx -Target D:\data\ycx
d----l 2026/5/13 10:51 ycx
dir 显示 d----l ycx
让 idea 始终以管理员身份启动:
idea64.exe 右键: 属性 -> 兼容性 -> 勾选以管理员身份运行此程序
Java程序实现创建链接目录,Windows 需要管理员权限,Linux正常
package ycx.test;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) {
try {
// ==================== 自动判断系统 ====================
boolean isWindows = System.getProperty("os.name").toLowerCase().contains("win");
// ==================== 链接路径 & 目标路径 ====================
Path linkPath; // 要创建的链接位置
Path targetPath; // 真实目标目录
if (isWindows) {
// Windows 对应命令:mklink /D D:\ycx D:\data\ycx
linkPath = Paths.get("D:\\ycx");
targetPath = Paths.get("D:\\data\\ycx");
} else {
// Linux 对应命令:ln -s /data/ycx /ycx
linkPath = Paths.get("/ycx");
targetPath = Paths.get("/data/ycx");
}
// ==================== Java 创建符号链接(跨平台) ====================
Files.createSymbolicLink(linkPath, targetPath);
System.out.println("✅ 软链接创建成功!");
System.out.println("链接位置:" + linkPath);
System.out.println("指向目标:" + targetPath);
} catch (Exception e) {
System.err.println("❌ 创建失败:" + e.getMessage());
e.printStackTrace();
}
}
}

浙公网安备 33010602011771号