每天一个Linux命令(30)ln命令
ln命令用来为文件创建链接,连接类型分为硬链接和符号链接两种,默认的连接类型是硬连接。如果要创建符号连接必须使用"-s"选项。
(1)用法:
用法: ln [options] source dist
(2)功能:
功能: 在文件之间建立连接
注意: 符号链接文件不是一个独立的文件,它的许多属性依赖于源文件,所以给符号链接文件设置存取权限是没有意义的。
(3)选项参数:
1) -s 软链接(符号链接)
2) -v 显示详细的处理过程
3) -d 允许超级用户制作目录的硬链接
(4)实例:
1)[root@localhost Documents]# ln -s findDir finDir_link 为目录创建软连接
[root@localhost Documents]# ll
总用量
dr--r--r--. root sunjimeng 5月 : findDir
drwxr-xr-x. root root 5月 : NoPdir
drwxr-xr-x. root root 5月 : Pdir
[root@localhost Documents]# ln -s findDir finDir_link
[root@localhost Documents]# ll
总用量
dr--r--r--. root sunjimeng 5月 : findDir
lrwxrwxrwx. root root 5月 : finDir_link -> findDir
drwxr-xr-x. root root 5月 : NoPdir
drwxr-xr-x. root root 5月 : Pdir
当源文件失效后,链接文件将失效。
[root@localhost Documents]# ll
总用量
dr--r--r--. root sunjimeng 5月 : findDir
lrwxrwxrwx. root root 5月 : finDir_link -> findDir //有效时的颜色
drwxr-xr-x. root root 5月 : NoPdir
drwxr-xr-x. root root 5月 : Pdir
[root@localhost Documents]# cd finDir_link
[root@localhost finDir_link]# ll
总用量
dr-xr-xr-x. root sunjimeng 5月 : Dir
[root@localhost findDir]# rmdir Dir
[root@localhost findDir]# cd ../
[root@localhost Documents]# rmdir findDir
[root@localhost Documents]# ll
总用量 0 //无效时的颜色
lrwxrwxrwx. root root 5月 : finDir_link -> findDir
drwxr-xr-x. root root 5月 : NoPdir
drwxr-xr-x. root root 5月 : Pdir
[root@localhost Documents]# cd finDir_link
bash: cd: finDir_link: 没有那个文件或目录
2)[root@localhost Documents]# ln newFile newLink 给文件创建硬链接
[root@localhost Documents]# ll
总用量
drwxr-xr-x. root root 5月 : NoPdir
drwxr-xr-x. root root 5月 : Pdir
[root@localhost Documents]# touch newFile //创建文件
[root@localhost Documents]# ln -s newFile newLink_s //创建文件符号链接
[root@localhost Documents]# ln newFile newLink //创建文件硬链接
[root@localhost Documents]# ln -s Pdir PdirLink_s //创建目录符号链接
[root@localhost Documents]# ln Pdir PdirLink //不允许创建目录硬链接
ln: "Pdir": 不允许将硬链接指向目录
[root@localhost Documents]# ll
总用量
-rw-r--r--. root root 5月 : newFile
-rw-r--r--. root root 5月 : newLink
lrwxrwxrwx. root root 5月 : newLink_s -> newFile
drwxr-xr-x. root root 5月 : NoPdir
drwxr-xr-x. root root 5月 : Pdir
lrwxrwxrwx. root root 5月 : PdirLink_s -> Pdir
创建的文件硬链接newLink与源文件newFile具有相同的权限,并且没有箭头。而文件软链接newLink_s的权限要多得多,而且有指向符号。
3)综合实例,比较硬链接与符号链接的差别
[root@localhost Documents]# cat >newFile <<EOF
> This is original file!
>
> I'm test the hard link and the symbol link!
> EOF //到这里新建一个文件
总用量
[root@localhost Documents]# ln -s newFile newFile_link_s
[root@localhost Documents]# ln newFile newFile_link
[root@localhost Documents]# rm newFile //删除源文件
rm:是否删除普通文件 "newFile"?y
[root@localhost Documents]# ll
总用量
-rw-r--r--. root root 5月 : newFile_link
lrwxrwxrwx. root root 5月 : newFile_link_s -> newFile
drwxr-xr-x. root root 5月 : NoPdir
drwxr-xr-x. root root 5月 : Pdir
[root@localhost Documents]# cat newFile_link //查看硬链接,完全不受影响,但符号链接已经失效
This is original file! I'm test the hard link and the symbol link!
[root@localhost Documents]# cat >newFile <<EOF 再新建一个文件newFile
> The Second Test!
>
> EOF
[root@localhost Documents]# ll
总用量
-rw-r--r--. root root 5月 : newFile
-rw-r--r--. root root 5月 : newFile_link
lrwxrwxrwx. root root 5月 : newFile_link_s -> newFile //符号链接已经恢复
drwxr-xr-x. root root 5月 : NoPdir
drwxr-xr-x. root root 5月 : Pdir
[root@localhost Documents]# cat newFile_link //分别查看符号链接和硬链接发现硬链接内容不变,符号链接内容变为新建的文件内容了。
This is original file! I'm test the hard link and the symbol link!
[root@localhost Documents]# cat newFile_link_s
The Second Test!
4)[root@localhost Documents]# ln newFile ln_dir 在另一个目录创建同名硬链接
[root@localhost Documents]# mkdir ln_dir
[root@localhost Documents]# ln newFile ln_dir
[root@localhost Documents]# cd ln_dir
[root@localhost ln_dir]# ll
总用量
-rw-r--r--. root root 5月 : newFile
修改newFile硬链接目录文件,也会导致源文件被修改。
5)[root@localhost Documents]# ln -sv a.c ./Pdir 在指定目录创建链接
[root@localhost Documents]# touch a.c
[root@localhost Documents]# ll
总用量
-rw-r--r--. root root 5月 : a.c
lrwxrwxrwx. root root 5月 : No_link -> NoPdir
drwxr-xr-x. root root 5月 : NoPdir
drwxr-xr-x. root root 5月 : Pdir
[root@localhost Documents]# ln -sv a.c ./Pdir
"./Pdir/a.c" -> "a.c"
[root@localhost Documents]# ln -sv a.c ./Pdir/b.c
"./Pdir/b.c" -> "a.c"
[root@localhost Documents]# ln -v a.c ./Pdir/c.c
"./Pdir/c.c" => "a.c"
[root@localhost Documents]# ls -l Pdir
总用量
lrwxrwxrwx. root root 5月 : a.c -> a.c
lrwxrwxrwx. root root 5月 : b.c -> a.c
-rw-r--r--. root root 5月 : c.c
-r--r--r--. root root 5月 : find
-rw-r--r--. root root 5月 : t3.txt
--w-------. root root 5月 : uText
-rw-r--r--. root root 5月 : vf
(5)其他:
扩展知识:
Linux具有为一个文件起多个名字的功能,称为链接。被链接的文件可以存放在相同的目录下,但是必须有不同的文件名,而不用在硬盘上为同样的数据重复备份。另外,被链接的文件也可以有相同的文件名,但是存放在不同的目录下,这样只要对一个目录下的该文件进行修改,就可以完成对所有目录下同名链接文件的修改。对于某个文件的各链接文件,我们可以给它们指定不同的存取权限,以控制对信息的共享和增强安全性。 文件链接有两种形式,即硬链接和符号链接。
硬链接:
建立硬链接时,在另外的目录或本目录中增加目标文件的一个目录项,这样,一个文件就登记在多个目录中。
创建硬链接后,己经存在的文件的I节点号(Inode)会被多个目录文件项使用。一个文件的硬链接数可以在目录的长列表格式的第二列中看到,无额外链接的文件的链接数为l。 在默认情况下,ln命令创建硬链接。ln命令会增加链接数,rm命令会减少链接数。一个文件除非链接数为0,否则不会从文件系统中被物理地删除。
对硬链接有如下限制:
1.不能对目录文件做硬链接。
2.不能在不同的文件系统之间做硬链接。就是说,链接文件和被链接文件必须位于同一个文件系统中。
软链接:
符号链接也称为软链接,是将一个路径名链接到一个文件。这些文件是一种特别类型的文件。事实上,它只是一个文本文件,其中包含它提供链接的另一个文件的路径名,如图中虚线箭头所示。另一个文件是实际包含所有数据的文件。所有读、写文件内容的命令被用于符号链接时,将沿着链接方向前进来访问实际的文件。
与硬链接不同的是,符号链接确实是一个新文件,当然它具有不同的I节点号;而硬链接并没有建立新文件。 符号链接没有硬链接的限制,可以对目录文件做符号链接,也可以在不同文件系统之间做符号链接。
用ln -s命令建立符号链接时,源文件最好用绝对路径名。这样可以在任何工作目录下进行符号链接。而当源文件用相对路径时,如果当前的工作路径与要创建的符号链接文件所在路径不同,就不能进行链接。 符号链接保持了链接与源文件或目录之间的区别: 删除源文件或目录,只删除了数据,不会删除链接。一旦以同样文件名创建了源文件,链接将继续指向该文件的新数据。 在目录长列表中,符号链接作为一种特殊的文件类型显示出来,其第一个字母是l。 符号链接的大小是其链接文件的路径名中的字节数。
每天一个Linux命令(30)ln命令的更多相关文章
- 每天一个linux命令(48)--ln命令
ln是Linux中又一个非常重要的命令,它的功能是为某个文件在另外一个位置建立一个同步的链接,当我们需要在不同的目录,用到相同的文件时,我们不需要在每个需要的目录下都放一个相同的文件,我们只要在某个固 ...
- linux常用命令:ln 命令
ln是linux中又一个非常重要命令,它的功能是为某一个文件在另外一个位置建立一个同步的链接.当我们需要在不同的目录,用到相同的文件时,我们不需要在每一个需要的目录下都放一个必须相同的文件,我们只要在 ...
- 【命令】ln命令
这是linux中一个非常重要命令,请大家一定要熟悉.它的功能是为某一个文件或目录在另外一个位置建立一个同步的链接,默认是链接是硬链接,常用参数是 "-s" . 对于ln命令,这里 ...
- 每天一个linux命令30)--chgrp命令
在Linux系统里,文件或目录的权限的掌控以拥有者及所属群组来管理.可以使用chgrp 指令取变更文件与目录所属群组,这种方式采用群组名称或群组识别码都可以. chgrp 命令就是change gr ...
- linux创建快捷方式ln命令
创建快捷方式命令 ln -s 源文件 目标目录 //目标目录可以是完整路径,也可以是当前目录下的路径 ln 源文件 目标目录 在桌面上添加一个,创建一个文件夹(这里是work)的快捷方式 //源 cd ...
- 【转载】每天一个Linux命令
目 录 每天一个linux命令(1) : ls 命令 每天一个linux命令(2) : cd 命令 每天一个linux命令(3) : pwd 命令 每天一个linux命令(4) : mkdi ...
- Linux ln命令:在文件之间建立链接(硬链接和软链接)详解版1
Linux ln命令:在文件之间建立链接(硬链接和软链接)详解版 < Linux创建文件及修改文件时间戳(touch命令)Linux复制文件和目录(cp命令) > <Linux就该这 ...
- linux ln 命令使用参数详解(ln -s 软链接)(转)
这是linux中一个非常重要命令,请大家一定要熟悉.它的功能是为某一个文件在另外一个位置建立一个同不的链接,这个命令最常用的参数是-s,具体用法是:ln -s 源文件 目标文件. 当 我们需要在不同的 ...
- Linux 软链接 硬链接 ln命令(繁杂版)
注意:创建软连接的时候,要用绝对路径!!! 这是linux中一个非常重要命令,请大家一定要熟悉.它的功能是为某一个文件在另外一个位置建立一个同不的链接,这个命令最常用的参数是-s,具体用法是:ln - ...
- Linux 命令之 ln
ln 的作用是制作一个文件或者目录的快捷方式,让我们在使用的过程当中更加方便地使用. 下面我来简单介绍一下 ln 的基本用法. ln 的基本语法 生成一个软链 ln -s source_name li ...
随机推荐
- 【SpringMVC学习09】SpringMVC与前台的json数据交互
json数据格式在接口调用中.html页面中比较常用,json格式比较简单,解析也比较方便,所以使用很普遍.在springmvc中,也支持对json数据的解析和转换,这篇文章主要总结一下springm ...
- .NET CORE 2.0小白笔记(四):asp.net core输出中文乱码的问题
问题描述:在学习asp.net core的时候,尝试在控制台,或者页面上输出中文,会出现乱码的问题. 分析解决:控制台乱码的原因是因为中文windows命令行默认编码页是gb2312,想输出中文只要把 ...
- 手动建立storybook
1. Add @storybook/react npm i --save-dev @storybook/react 2. Add react, react-dom, and babel-core np ...
- Unmapped Spring configuration files found.
© 版权声明:本文为博主原创文章,转载请注明出处 1.问题描述: 搭建SSH框架后,IDEA弹出如下提示: 2.解决方案: File --> Project Structure --> M ...
- zoj 3827 Information Entropy 【水题】
Information Entropy Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge Information ...
- Spring Cloud Zuul 网关的分布式系统中整合Swagger(转)和 zuul跨域访问问题
首先恭喜自己终于找对了努力的方向,很荣幸能在公司接触到微服务架构,也很高兴公司一个大佬哥们愿意带我,他技术确实很牛逼,我也很佩服他,前后端通吃,干了六年能有这样的水平.最近跟着在搞微服务架构,给我分配 ...
- Google Code Jam 2014 资格赛:Problem C. Minesweeper Master
Problem Minesweeper is a computer game that became popular in the 1980s, and is still included in so ...
- 多媒体开发之rtp 打包发流---同网段其他机子sdp 播放不了
(1) (2) (3) -------------author:pkf ------------------time:2015-1-6 后面发现是connection 的server 地址是指定的 导 ...
- saltstack之软件管理
1.installed安装软件包 例: 安装NFS /srv/salt/pkg/nfs.sls nfs: pkg.installed: - pkgs: - nfs-utils 在命令行执行如下 sal ...
- 解决QT:forward declaration of 'struct Ui::xxx';invalid use of incomplete struct "Ui::Widget" 等莫名奇异错误
今天在进行QT Widget的UI设计时,改了下Widget的对象名,然后在多次成功编译执行后,执行清理,又一次构建,就出现了好多莫名奇异的错误: widget.h:12: 错误:forward de ...