linux亦步亦趋(08)文件处理ln命令

 ln命令:链接文件

分为硬链接和软连接,其格式分别如下:

ln -s srcfile desfile; ln -s  源文件  目的文件(软连接文件);

ln srcfile desfile;ln 源文件  目的文件(软连接文件);

软连接:

我们通过简单的实例来分析一下他们的用法和特性:

[root@localhost etc]# ln -s /etc/hosts /hosts.s
[root@localhost etc]# ls -l /etc/hosts /hosts.s
-rw-r--r-- 2 root root 187 07-16 15:00 /etc/hosts
lrwxrwxrwx 1 root root  10 07-22 12:06 /hosts.s -> /etc/hosts
[root@localhost etc]#

 我们首先创建了一个软连接文件/hosts.s  然后又用ls查看了源文件和软连接文件的文件属性,可以发现几个特点

  • 大小,源文件和软连接文件的大小不一样,源文件很大而软连接文件很小。
  • 权限,软连接权限是全部开放的。
  • 软连接文件属性第一个字符是l。
  • 创建时间也不一样,源文件和软连接文件的创建时间也不一样。

其实软连接就有和我们windows里面的快捷方式一模一样,其显示的权限没有任何意义,最终的权限还是由源文件控制。

硬连接:

我们通过简单的实例来分析一下:

[root@localhost etc]# ln /etc/hosts /hosts.h
[root@localhost etc]# ls -l /etc/hosts /hosts.h
-rw-r--r-- 3 root root 0 07-22 12:07 /etc/hosts
-rw-r--r-- 3 root root 0 07-22 12:07 /hosts.h

我们可以发现如下特点:

  • 硬连接的文件属性开头不是l,
  • 大小和源文件一样大;
  • 修改时间是一样的。

其实硬连接文件就是一个文件的复本,当你改变其中的一个文件的时候另一个也会改变,一般用于备份容易被别人误删除的文件,因为他与源文件的inode是一样的。

查看inode:

[root@localhost etc]# ls -li /etc/hosts /hosts.h
2009804 -rw-r--r-- 3 root root 0 07-22 12:07 /etc/hosts
2009804 -rw-r--r-- 3 root root 0 07-22 12:07 /hosts.h

注意:不可以跨文件系统建立硬连接,如下例子:

[root@localhost etc]# ln /etc/hosts /studyData/hosts.h
ln: 正在创建指向“/etc/hosts”的硬链接“/studyData/hosts.h”: 无效的跨设备连接
[root@localhost etc]#

猜你喜欢

转载自xuelianbobo.iteye.com/blog/2094920