每天2个Linux命令 ln diff


2017-07-28 每天2个Linux命令 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
    总用量 0
    dr--r--r--. 3 root sunjimeng 16 5月  24 07:52 findDir
    drwxr-xr-x. 2 root root      51 5月  21 07:10 NoPdir
    drwxr-xr-x. 2 root root      51 5月  21 07:09 Pdir
    [root@localhost Documents]# ln -s findDir finDir_link
    [root@localhost Documents]# ll
    总用量 0
    dr--r--r--. 3 root sunjimeng 16 5月  24 07:52 findDir
    lrwxrwxrwx. 1 root root       7 5月  27 06:04 finDir_link -> findDir
    drwxr-xr-x. 2 root root      51 5月  21 07:10 NoPdir
    drwxr-xr-x. 2 root root      51 5月  21 07:09 Pdir
    复制代码
          当源文件失效后,链接文件将失效。

    复制代码
    [root@localhost Documents]# ll
    总用量 0
    dr--r--r--. 3 root sunjimeng 16 5月  24 07:52 findDir
    lrwxrwxrwx. 1 root root       7 5月  27 06:04 finDir_link -> findDir    //有效时的颜色
    drwxr-xr-x. 2 root root      51 5月  21 07:10 NoPdir
    drwxr-xr-x. 2 root root      51 5月  21 07:09 Pdir
    [root@localhost Documents]# cd finDir_link
    [root@localhost finDir_link]# ll
    总用量 0
    dr-xr-xr-x. 3 root sunjimeng 60 5月  24 08:01 Dir
    [root@localhost findDir]# rmdir Dir
    [root@localhost findDir]# cd ../
    [root@localhost Documents]# rmdir findDir
    [root@localhost Documents]# ll
    总用量 0                                   //无效时的颜色
    lrwxrwxrwx. 1 root root  7 5月  27 06:04 finDir_link -> findDir 
    drwxr-xr-x. 2 root root 51 5月  21 07:10 NoPdir
    drwxr-xr-x. 2 root root 51 5月  21 07:09 Pdir
    [root@localhost Documents]# cd finDir_link
    bash: cd: finDir_link: 没有那个文件或目录

2)[root@localhost Documents]# ln newFile newLink          给文件创建硬链接

复制代码
[root@localhost Documents]# ll
总用量 0
drwxr-xr-x. 2 root root 51 5月  21 07:10 NoPdir
drwxr-xr-x. 2 root root 51 5月  21 07:09 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
总用量 0
-rw-r--r--. 2 root root  0 5月  27 06:18 newFile
-rw-r--r--. 2 root root  0 5月  27 06:18 newLink          
lrwxrwxrwx. 1 root root  7 5月  27 06:19 newLink_s -> newFile
drwxr-xr-x. 2 root root 51 5月  21 07:10 NoPdir
drwxr-xr-x. 2 root root 51 5月  21 07:09 Pdir
lrwxrwxrwx. 1 root root  4 5月  27 06:19 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                                                     //到这里新建一个文件
总用量 4
[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
总用量 4
-rw-r--r--. 1 root root 68 5月  27 06:30 newFile_link    
lrwxrwxrwx. 1 root root  7 5月  27 06:31 newFile_link_s -> newFile
drwxr-xr-x. 2 root root 51 5月  21 07:10 NoPdir
drwxr-xr-x. 2 root root 51 5月  21 07:09 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
总用量 8
-rw-r--r--. 1 root root 18 5月  27 06:33 newFile
-rw-r--r--. 1 root root 68 5月  27 06:30 newFile_link
lrwxrwxrwx. 1 root root  7 5月  27 06:31 newFile_link_s -> newFile   //符号链接已经恢复
drwxr-xr-x. 2 root root 51 5月  21 07:10 NoPdir
drwxr-xr-x. 2 root root 51 5月  21 07:09 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
总用量 4
-rw-r--r--. 2 root root 18 5月  27 06:33 newFile

5)[root@localhost Documents]# ln -sv a.c ./Pdir           在指定目录创建链接

复制代码
[root@localhost Documents]# touch a.c
[root@localhost Documents]# ll
总用量 0
-rw-r--r--. 1 root root  0 5月  27 07:03 a.c
lrwxrwxrwx. 1 root root  6 5月  27 06:58 No_link -> NoPdir
drwxr-xr-x. 2 root root 51 5月  21 07:10 NoPdir
drwxr-xr-x. 2 root root 51 5月  21 07:09 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
总用量 8
lrwxrwxrwx. 1 root root   3 5月  27 07:04 a.c -> a.c
lrwxrwxrwx. 1 root root   3 5月  27 07:04 b.c -> a.c
-rw-r--r--. 2 root root   0 5月  27 07:03 c.c
-r--r--r--. 1 root root   0 5月  19 04:16 find
-rw-r--r--. 1 root root  85 5月  19 04:25 t3.txt
--w-------. 1 root root   0 5月  15 18:34 uText
-rw-r--r--. 1 root root 105 5月  21 06:35 vf

(5)其他:

  扩展知识:

  Linux具有为一个文件起多个名字的功能,称为链接。被链接的文件可以存放在相同的目录下,
    但是必须有不同的文件名,而不用在硬盘上为同样的数据重复备份。另外,被链接的文件也可以有相同的文件名,
    但是存放在不同的目录下,这样只要对一个目录下的该文件进行修改,就可以完成对所有目录下同名链接文件的修改。
    对于某个文件的各链接文件,我们可以给它们指定不同的存取权限,以控制对信息的共享和增强安全性。
     文件链接有两种形式,即硬链接和符号链接。
  硬链接:

  建立硬链接时,在另外的目录或本目录中增加目标文件的一个目录项,这样,一个文件就登记在多个目录中。

  创建硬链接后,己经存在的文件的I节点号(Inode)会被多个目录文件项使用。
    一个文件的硬链接数可以在目录的长列表格式的第二列中看到,无额外链接的文件的链接数为l。 
在默认情况下,ln命令创建硬链接。ln命令会增加链接数,rm命令会减少链接数。一个文件除非链接数为0,
否则不会从文件系统中被物理地删除。

  对硬链接有如下限制:

  1.不能对目录文件做硬链接。

  2.不能在不同的文件系统之间做硬链接。就是说,链接文件和被链接文件必须位于同一个文件系统中。
  软链接:

  符号链接也称为软链接,是将一个路径名链接到一个文件。这些文件是一种特别类型的文件。
    事实上,它只是一个文本文件,其中包含它提供链接的另一个文件的路径名,如图中虚线箭头所示。
另一个文件是实际包含所有数据的文件。所有读、写文件内容的命令被用于符号链接时,将沿着链接方向前进来访问实际的文件。

  与硬链接不同的是,符号链接确实是一个新文件,当然它具有不同的I节点号;而硬链接并没有建立新文件。
 符号链接没有硬链接的限制,可以对目录文件做符号链接,也可以在不同文件系统之间做符号链接。
  用ln -s命令建立符号链接时,源文件最好用绝对路径名。这样可以在任何工作目录下进行符号链接。
而当源文件用相对路径时,如果当前的工作路径与要创建的符号链接文件所在路径不同,就不能进行链接。 
符号链接保持了链接与源文件或目录之间的区别: 删除源文件或目录,只删除了数据,不会删除链接。
一旦以同样文件名创建了源文件,链接将继续指向该文件的新数据。 在目录长列表中,
符号链接作为一种特殊的文件类型显示出来,其第一个字母是l。 符号链接的大小是其链接文件的路径名中的字节数。

2017-07-28 每天2个Linux命令 diff命令

diff命令在最简单的情况下,比较给定的两个文件的不同。如果使用“-”代替“文件”参数,则要比较的内容将来自标准输入。
diff命令是以逐行的方式,比较文本文件的异同处。如果该命令指定进行目录的比较,
则将会比较该目录中具有相同文件名的文件,而不会对其子目录文件进行任何比较操作。

(1)用法:

用法:  diff  [选项参数]  [文件1或目录1] [文件2或目录2]

(2)功能:

功能:  diff命令能比较单个文件或者目录内容。如果指定比较的是文件,则只有当输入为文本文件时才有效。
以逐行的方式,比较文本文件的异同处。如果指定比较的是目录的的时候,diff 命令会比较两个目录下名字相同的文本文件。
列出不同的二进制文件、公共子目录和只在一个目录出现的文件。

(3)选项参数:

  1) -y  --side-by-side            以并列的方式显示文件的异同之处。

  2) -W --width                在使用-y参数时,指定栏宽。

  3) -c                    显示全部内文,并标出不同之处。

  4) -u -U --unified              以合并的方式来显示文件内容的不同。

  5) -r --recursive              比较子目录中的文件

  6) -n --rcs                 将比较结果以RCS的格式来显示。

(4)实例:

  1)[root@localhost Document]# diff t1.txt t2.txt        比较两个文档的区别

复制代码
[root@localhost Document]# cat >t1.txt <<EOF   //第一种新建文档的方式
> this is a text!
> 
> Name is t1.txt!
> The extra content!
> I am MenAngel!
> EOF
[root@localhost Document]# cat >t2.txt        //第二种新建文档的方式
this is a text!

Name is t2.txt!
^Z
[2]+  已停止               cat > t2.txt       //按ctrl+z键停止
[root@localhost Document]# diff ../Document/t1.txt ../Document/t2.txt    diff后的两个文件参数可以跟相对路径也可以跟绝对路径
3,5c3                                  
< Name is t1.txt!
< The extra content!
< I am MenAngel!
---
> Name is t2.txt!
[root@localhost Document]# diff t1.txt t2.txt
3,5c3
< Name is t1.txt!
< The extra content!
< I am MenAngel!
---
> Name is t2.txt!
复制代码
      第一个3表示两个文档第3行不同,第二个5c3表示第一个文档有5行,而第2个文档有三行。

2)[root@localhost Document]# diff -y t1.txt t2.txt        以并排显示比较两个文档的区别

[root@localhost Document]# diff -y t1.txt t2.txt
this is a text!                            this is a text!

Name is t1.txt!                              |    Name is t2.txt!
The extra content!                          <
I am MenAngel!                              <

3)[root@localhost Document]# diff -y -W 40 t1.txt t2.txt    在(2)的基础上自定义显示的宽度

[root@localhost Document]# diff -y -W 40 t1.txt t2.txt
this is a text!        this is a text!

Name is t1.txt!       |    Name is t2.txt!
The extra conten   <
I am MenAngel!     <

4)[root@localhost Document]# diff -y -W 40 t1.txt t2.txt 或者 t2.txt t1.txt    文档顺序对结果的影响

复制代码
[root@localhost Document]# diff -y -W 40 t1.txt t2.txt
this is a text!        this is a text!

Name is t1.txt!       |    Name is t2.txt!
The extra conten   <
I am MenAngel!       <
[root@localhost Document]# diff -y -W 40 t2.txt t1.txt
this is a text!        this is a text!

Name is t2.txt!       |    Name is t1.txt!
           >    The extra conten
           >    I am MenAngel!
复制代码
  说明:

  “|”表示前后2个文件内容有不同

  “<”表示后面文件比前面文件少了1行内容

  “>”表示后面文件比前面文件多了1行内容

5)[root@localhost Document]# diff -c t1.txt t2.txt       
将进行比较的两个文档的内容全部显示出来标明行数,标出不同点

复制代码
[root@localhost Document]# diff -c t1.txt t2.txt
*** t1.txt    2016-05-27 23:31:25.949100752 -0700
--- t2.txt    2016-05-27 23:31:54.287100555 -0700
***************
*** 1,5 ****           //从1到5行
  this is a text!

! Name is t1.txt!     //第3.4.5行不同
! The extra content!  //由于t2.txt没有第4和第5行,所以第4和第5行表明是比t2.txt多的
! I am MenAngel!
--- 1,3 ----          //从1到3行
  this is a text!

! Name is t2.txt!     //第三行不同 
复制代码
  说明:

  这种方式在开头两行作了比较文件的说明,这里有三中特殊字符:

    (“+” 比较的文件的后者比前着多一行

    “-” 比较的文件的后者比前着少一行)  //-u参数用的

    “!” 比较的文件两者有差别的行

6)[root@localhost Document]# diff -u t1.txt t2.txt        以合并的方式显示文本的不同      

复制代码
[root@localhost Document]# diff -u t1.txt t2.txt                        //它的第一部分,也是文件的基本信息
--- t1.txt    2016-05-27 23:31:25.949100752 -0700                       //-号表示第一个文件
+++ t2.txt    2016-05-27 23:31:54.287100555 -0700                       //+号表示第二个文件
@@ -1,5 +1,3 @@
 this is a text!

-Name is t1.txt!
-The extra content!
-I am MenAngel!
+Name is t2.txt!   //每个减号对应一个加号,如果没有对应则表明在另一个文件中没有此行

7)[root@localhost Document]# diff dir1 dir2            比较两个目录

复制代码
[root@localhost Document]# mkdir dir1 dir2                                 //创建两个目录
[root@localhost Document]# cd dir1
[root@localhost dir1]# cat >text1 <<EOF    //在dir1中创建text1,text2在dir2中创建text1,text3
> dir:      dir1
> name:     text1
> 
> Total 4!
> EOF
[root@localhost dir1]# cat >text2 <<EOF
> I am MenAngel!
> I am studying the order of Linux!
> EOF
[root@localhost dir1]# cd ../dir2
[root@localhost dir2]# cat >text1 <<EOF
> dir:      dir2
> name:     text1
> 
> 
> Total 5!
> EOF
[root@localhost dir2]# cat >text3 <<EOF
> Working hard makes success!
> I am MenAngel!
> EOF
[root@localhost dir2]# cd ../
[root@localhost Document]# diff dir1 dir2
只在 dir2 存在:text3
diff dir1/text1 dir2/text1       //遇到同名文件自动比较
1c1
< dir:      dir1
---
> dir:      dir2
4c4,5
< Total 4!
---
> 
> Total 5!
只在 dir1 存在:text2

8)[root@localhost Document]# diff dir1 dir2 >dir.log      
产生补丁,其实就是把原本要输出到标准输出的内容输出到自定义文件中

复制代码
[root@localhost Document]# diff dir1 dir2 >dir.log
[root@localhost Document]# cat dir.log
只在 dir2 存在:text
diff dir1/text1 dir2/text1
1c1
< dir:      dir1
---
> dir:      dir2
4c4,5
< Total 4!
---
> 
> Total 5!
只在 dir1 存在:text2

9)[root@localhost Document]# diff t1.txt t2.txt>t12.log        产生补丁,并用此补丁更新文件

复制代码
[root@localhost Document]# cat t1.txt t2.txt
this is a text!

Name is t1.txt!
The extra content!
I am MenAngel!      //到这是t1.txt的内容
this is a text!

Name is t2.txt!    //到这是t2.txt的内容
[root@localhost Document]# diff t1.txt t2.txt>t12.log       //产生补丁
[root@localhost Document]# patch t1.txt t12.log             //给t1.txt打补丁,让它的内容变为t2.txt的内容
patching file t1.txt
[root@localhost Document]# cat t1.txt
this is a text!

Name is t2.txt!
[root@localhost Document]# patch t2.txt t12.log             //给t2.txt打补丁,不过好像有点差别
patching file t2.txt
Reversed (or previously applied) patch detected!  Assume -R? [n] y    //还不知道是为什么?
[root@localhost Document]# cat t2.txt
this is a text!

Name is t1.txt!
The extra content!
I am MenAngel!
复制代码

(5)其他:

  扩展知识:

  diff 命令是 linux上非常重要的工具,用于比较文件的内容,特别是比较两个版本不同的文件以找到改动的地方。
diff在命令行中打印每一个行的改动。最新版本的diff还支持二进制文件。diff程序的输出被称为补丁 (patch),
因为Linux系统中还有一个patch程序,可以根据diff的输出将a.c的文件内容更新为b.c。
diff是svn、cvs、git等版本控制工具不可或缺的一部分。

文章作者: 邓滔
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 邓滔 !
评论
  目录