readlink命令用来显示符号链接所指向的位置。
sh-# readlink /bin/cp
/openbox/bin/cp
sh-#
sh-# echo $?
0
sh-#
sh-# readlink /open/bin/cp
sh-#
sh-# echo $?
1
sh-#
使用ls -l命令也可以查看一个档案是否是链接档案,
sh-# ls -l /openbox/bin/cp
-rwxr-xr-x 1 root root 56132 Oct 17 2013 /openbox/bin/cp
sh-#
sh-# ls -l /bin/cp
lrwxrwxrwx 1 root root 15 Oct 17 2013 /bin/cp -> /openbox/bin/cp
sh-#
readlink也是一个系统调用,其作用和readlink命令一样。
#include <unistd.h>
ssize_t readlink(const char *path, char *buf, size_t bufsiz);
接下来实验验证readlink命令,
sh-# touch /tmp/link_test
sh-# ln -s /tmp/link_test /tmp/link1
sh-# ln -s /tmp/link1 /tmp/link2
sh-# readlink /tmp/link2
/tmp/link1
sh-#
sh-# readlink -f /tmp/link2
/tmp/link_test
sh-#
sh-# ls -l /tmp/link2
lrwxrwxrwx 1 root root 10 Jan 1 00:06 /tmp/link2 -> /tmp/link1
sh-#
sh-# ls -l /tmp/link1
lrwxrwxrwx 1 root root 14 Jan 1 00:06 /tmp/link1 -> /tmp/link_test
sh-#
sh-# ls -l /tmp/link_test
-rw-r--r-- 1 root root 0 Jan 1 00:04 /tmp/link_test
sh-#
sh-# readlink /tmp/link_test
sh-# echo $?
1
sh-#
sh-# ln /tmp/link_test /tmp/link3
sh-# ls -l /tmp/link3
-rw-r--r-- 2 root root 0 Jan 1 00:04 /tmp/link3
sh-#
sh-# busybox readlink /tmp/link3
sh-#
sh-# echo $?
1
sh-#
关于符号链接和硬链接,后续会找机会在其它章节中进行学习。