readlink:查看符号链接文件的内容

xiaohai 2021-05-05 16:53:55 1604人围观 标签: Linux 
简介在创建了软链接后,如果要查看软链接文件的内容就需要使用该命令。如果是cat命令查看,就只能看到源文件的内容,而看不到真正的内容。
说明:

  在创建了软链接后,如果要查看软链接文件的内容就需要使用该命令。如果是cat命令查看,就只能看到源文件的内容,而看不到真正的内容。

格式:

  readlink [option] [file]
  readlink [选项] [文件]

常用参数

  -f:可以递归跟随给出文件名的所有符号链接以标准化,除最后一个外所有组件必须存在。我的理解就是显示绝对源文件的绝对路径

示例

[root@localhost test]# ll
总用量 4
lrwxrwxrwx. 1 root root  9 5月   3 07:30 abc1.txt -> test5.txt
drwxr-xr-x. 4 root root 56 5月   3 07:36 dir01
drwxr-xr-x. 2 root root  6 5月   3 07:00 dir02
drwxr-xr-x. 2 root root  6 5月   3 07:00 dir03
drwxr-xr-x. 2 root root  6 5月   3 07:00 dir04
drwxr-xr-x. 2 root root  6 5月   3 07:00 dir05
-rw-r--r--. 1 root root  0 5月   3 07:00 test2.txt
-rw-r--r--. 1 root root  0 5月   3 07:00 test3.txt
-rw-r--r--. 1 root root  0 5月   3 07:00 test4.txt
-rw-r--r--. 1 root root  2 5月   3 08:42 test5.txt
[root@localhost test]# cat abc1.txt  #使用cat查看链接文件,结果是内容
1
[root@localhost test]# readlink abc1.txt  #使用readlink查看,真实的内容
test5.txt
[root@localhost test]# readlink -f abc1.txt  #加上-f,显示源文件的绝地路径
/root/test/test5.txt
[root@localhost test]# ln -s ../test/test2.txt abc2.txt #创建一个新的链接文件
[root@localhost test]# ll
总用量 4
lrwxrwxrwx. 1 root root  9 5月   3 07:30 abc1.txt -> test5.txt
lrwxrwxrwx. 1 root root 17 5月   3 08:46 abc2.txt -> ../test/test2.txt
drwxr-xr-x. 4 root root 56 5月   3 07:36 dir01
drwxr-xr-x. 2 root root  6 5月   3 07:00 dir02
drwxr-xr-x. 2 root root  6 5月   3 07:00 dir03
drwxr-xr-x. 2 root root  6 5月   3 07:00 dir04
drwxr-xr-x. 2 root root  6 5月   3 07:00 dir05
-rw-r--r--. 1 root root  0 5月   3 07:00 test2.txt
-rw-r--r--. 1 root root  0 5月   3 07:00 test3.txt
-rw-r--r--. 1 root root  0 5月   3 07:00 test4.txt
-rw-r--r--. 1 root root  2 5月   3 08:42 test5.txt
[root@localhost test]# readlink abc2.txt #显示真实的内容
../test/test2.txt
[root@localhost test]# readlink -f abc2.txt  #加上-f,显示源文件的绝地路径
/root/test/test2.txt