文章转自https://zhidao.baidu.com/question/579980682.html
使用 ls -l
即可
例子:
$ touch file1 # 创建新文件 file1
$ touch file2 # 创建新文件 file2
$ ln file1 file3 # 为 file1 创建硬链接 file3
$ ls -l
total 0
-rw-r--r-- 2 root root 0 Aug 12 16:59 file1
-rw-r--r-- 1 root root 0 Aug 12 17:00 file2
-rw-r--r-- 2 root root 0 Aug 12 16:59 file3
数字大于2,即为硬链接
补充:
1)使用
ls -i # 可以与 ls -l 一起使用, 即 ls -il
可以查看 inode number
$ ls -il
total 0
267105 -rw-r--r-- 2 root root 0 Aug 12 16:59 file1
267106 -rw-r--r-- 1 root root 0 Aug 12 17:00 file2
267105 -rw-r--r-- 2 root root 0 Aug 12 16:59 file3
这时结果的第一列就是文件的 inode number, 可以看出由于 file1 和 file3 互为硬链接, 所以他们的 inode number 相同.
2)如何找出所有硬链接到某个文件的文件?
首先使用
ls -i
查看 inode number
然后使用
find -inum
查找所有指向该inode的文件
例子:
$ find . -inum 267105
./file3
./file1
- 关于文件夹
文件夹没有硬链接, 只有符号链接
- 关于 NTFS
微软的文件系统好像只有 NTFS 支持硬链接.
在 NTFS 文件系统上, 当该文件是长文件名(非8.3标准dos格式)时, 而且该文件在 Windows 上创建时, 硬链接数会自动增加 1. (在 NTFS 文件系统 ls -l, 你会发现长文件名文件的 "硬链接数"通常是 2). 当然如果你创建硬链接, 那个数字也会增加. 这个与 Windows 处理长文件名文件的方式有关, Windows 为了兼容性, 会为每个长文件名文件创建一个8.3格式短文件名的硬链接. 这时虽然某个文件可能有2个硬链接, 但 Linux知道他们其实只是一个文件的不同名字, 在用户空间会把它们当作是一个文件, 你用 ls 或是 find 也只能找到1个文件, 你删除了1个就两个都没有了, 不能算是真正的硬链接. 所以在 NFTS 文件系统下, 该方法可能效果并不好. 不过在 Linux 下处理 NTFS 硬链接应该本来就是 Linux 的非典型应用了