linux readlink命令学习

本文详细解释了Linux环境下使用readlink命令来显示符号链接所指向的位置,并通过实验验证了其功能。同时,文章还展示了如何利用ls-l命令检查文件是否为链接文件,以及readlink命令与系统调用之间的联系。最后,通过一系列操作,如创建链接、验证链接等,深入探讨了符号链接和硬链接的概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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-#

关于符号链接和硬链接,后续会找机会在其它章节中进行学习。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值