在 CentOS(以及其他 Linux 发行版)中,“软连接”也就是符号链接(symbolic link,symlink)。下面按常见使用场景给你总结几种查找方法。
ls -lls -l /path/to/file
如果是软连接,会看到类似:
lrwxrwxrwx 1 root root 10 6月 1 10:00 file -> /real/file
开头的 l 表示软连接。
file 命令file /path/to/file
输出示例:
file: symbolic link to `/real/file'
readlinkreadlink /path/to/file
只输出软连接指向的真实路径。
find 查找软连接find /path/to/dir -type l
示例:查找 /etc 下所有软连接
find /etc -type l
find /path/to/dir -type l -exec ls -l {} \;
例如,查找所有指向 /opt/app 的软连接:
find / -type l -lname '*opt/app*' 2>/dev/null
或者更精确:
find / -type l -exec sh -c 'readlink "{}" | grep -q "/opt/app"' \; -print 2>/dev/null
find /path/to/dir -type l ! -exec test -e {} \; -print
或更简单:
find /path/to/dir -xtype l
which python
ls -l /usr/bin/python
很多命令其实是软连接,例如:
/usr/bin/python -> python3
| 目的 | 命令 |
|---|---|
| 查看是否软连接 | ls -l |
| 查看指向 | readlink |
| 查找软连接 | find -type l |
| 查找断链 | find -xtype l |
| 查看命令软连接 | ls -l $(which cmd) |
如果你有具体场景(例如:查找某个服务的软连接、查找误删的软连接、批量处理软连接),可以告诉我,我可以给你更针对性的命令。