iis服务器助手广告广告
返回顶部
首页 > 资讯 > 操作系统 >linux中diff命令有什么用
  • 575
分享到

linux中diff命令有什么用

2023-06-09 17:06:12 575人浏览 薄情痞子
摘要

这篇文章主要为大家展示了“linux中diff命令有什么用”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“linux中diff命令有什么用”这篇文章吧。这又是一个文件比较命令,熟练掌握会节省你很多

这篇文章主要为大家展示了“linux中diff命令有什么用”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“linux中diff命令有什么用”这篇文章吧。

这又是一个文件比较命令,熟练掌握会节省你很多工作。还记得comm命令吗?diff比他更加复杂,好用,不仅能比较文件,还能比较目录。 当一个文件有多个版本时候,或者更多复杂的文件,目录比较时,你会期望有一个比comm更有用的命令,diff正是为此而生。

全称:different file

实例:

文件1: v1.txt

代码如下:

hello
  world v1_echo
  v1_diff
  v1_comm
 shell is easy!

文件2: v2.txt

代码如下:

hello world!
v2_echo
 v1_diff
 easy shell!

1.普通比较:
$diff v1.txt v2.txt

代码如下:

1,2c1,2
 < hello world
 &lt; v1_echo
 ---
 > hello world!
 &gt; v2_echo
 4c4
 < v1_comm
 ---
 > easy shell!

列出比较的区别,数字为行数,c是compare

2.一体化输出:

$diff -u v1.txt v2.txt &gt; version.patch

代码如下:

-v1_echo
 +hello world!
 +v2_echo
 v1_diff
 -v1_comm
 +easy shell!
 shell is easy!

"+"表示补充的行,"-"表示删去的行

3. 更新覆盖v1.txt,注意这里借用了patch

$patch –p1 v1.txt&lt; version.patch

4. 比较v1,v2文件,仅输出v2的变化

$diff -e v1.txt v2.txt

代码如下:

 4c
 easy shell!
 .
 1,2c
 hello world!
 v2_echo
 .

5.比较目录

借用书上一段解释:

-N 将所有确实的文件视为空文件
-a 将所有文件市委文件文本
-u 生成一体化输出
-r 遍历目录下的所有文件

比较两个目录下所有文本信息

$diff –Naur directory1 directory2

除非你确定这两个目录基本相同,不然不推荐这么用。

6.工作疲劳后,尤其同时做很多事情的时候,会忘了有没有做过上传,或者修改某个目录下的文件。可以这样:

$diff v1.txt /${dir}

直接比较同名文件,高强度下的工作者相信真有体会。

解读下—help

太长,截取少部分解读一下。

代码如下:

Mandatory arguments to long options are mandatory for short options too.
 --nORMal                                        output a normal diff (the default)
 -q, --brief                                      report only when files differ
 -s, --report-identical-files               report when two files are the same
 -c, -C NUM, --context[=NUM]   output NUM (default 3) lines of copied context
 -u, -U NUM, --unified[=NUM]     output NUM (default 3) lines of unified context
 -e, --ed                                          output an ed script
 -n, --rcs                                         output an RCS format diff
 -y, --side-by-side                           output in two columns

FILES 的格式可以是‘FILE1 FILE2'、‘DIR1 DIR2'、‘DIR FILE...'或
‘FILE... DIR'。

解读:

1.-q –s

-q: 只有两个不同是,提示“两个文件不同”

-s: 两个文件相同时,提示“两个文件相同”

$diff -qs v1.txt v2.txt

(有点无聊的命令 )

2. –y

按照列的方式输出,增加了直观性

代码如下:

 hello world               | hello world!
 v1_echo                    | v2_echo
 v1_diff                       v1_diff
 v1_comm                   | easy shell!
 shell is easy!               shell is easy!

3. file 后面的格式

可以是1.文件 文件 2.目录与目录 3.文件与目录 4. 目录与文件
解释一下,文件与目录的形式,比较的是该目录下 与 比较文件同名的文件比较
如:$ diff v1.txt diff/
则实际是v1.txt 与 diff/v1.txt的比较。这是个很常用的功能。
对比comm
就比较文件而言,comm功能比较简单,但是也很直观,方便
而diff 提供了更强大,更复杂的比较方式,还能对比目录,遍历目录。

代码如下:

用法:diff [选项]... FILES
Compare FILES line by line.
Mandatory arguments to long options are mandatory for short options too.
      --normal                  output a normal diff (the default)
  -q, --brief                   report only when files differ
  -s, --report-identical-files  report when two files are the same
  -c, -C NUM, --context[=NUM]   output NUM (default 3) lines of copied context
  -u, -U NUM, --unified[=NUM]   output NUM (default 3) lines of unified context
  -e, --ed                      output an ed script
  -n, --rcs                     output an RCS format diff
  -y, --side-by-side            output in two columns
  -W, --width=NUM               output at most NUM (default 130) print columns
      --left-column             output only the left column of common lines
      --suppress-common-lines   do not output common lines

  -p, --show-c-function         show which C function each change is in
  -F, --show-function-line=RE   show the most recent line matching RE
      --label LABEL             use LABEL instead of file name
                                  (can be repeated)

  -t, --expand-tabs             expand tabs to spaces in output
  -T, --initial-tab             make tabs line up by prepending a tab
      --tabsize=NUM             tab stops every NUM (default 8) print columns
      --suppress-blank-empty    suppress space or tab before empty output lines
  -l, --paginate                pass output through `pr' to paginate it

  -r, --recursive                 recursively compare any subdirectories found
  -N, --new-file                  treat absent files as empty
      --unidirectional-new-file   treat absent first files as empty
      --ignore-file-name-case     ignore case when comparing file names
      --no-ignore-file-name-case  consider case when comparing file names
  -x, --exclude=PAT               exclude files that match PAT
  -X, --exclude-from=FILE         exclude files that match any pattern in FILE
  -S, --starting-file=FILE        start with FILE when comparing directories
      --from-file=FILE1           compare FILE1 to all operands;
                                    FILE1 can be a directory
      --to-file=FILE2             compare all operands to FILE2;
                                    FILE2 can be a directory

  -i, --ignore-case               ignore case differences in file contents
  -E, --ignore-tab-expansion      ignore changes due to tab expansion
  -Z, --ignore-trailing-space     ignore white space at line end
  -b, --ignore-space-change       ignore changes in the amount of white space
  -w, --ignore-all-space          ignore all white space
  -B, --ignore-blank-lines        ignore changes whose lines are all blank
  -I, --ignore-matching-lines=RE  ignore changes whose lines all match RE

  -a, --text                      treat all files as text
      --strip-trailing-cr         strip trailing carriage return on input

  -D, --ifdef=NAME                output merged file with `#ifdef NAME' diffs
      --GTYPE-group-format=GFMT   format GTYPE input groups with GFMT
      --line-format=LFMT          format all input lines with LFMT
      --LTYPE-line-format=LFMT    format LTYPE input lines with LFMT
    These format options provide fine-grained control over the output
      of diff, generalizing -D/--ifdef.
    LTYPE 可以是‘old'、‘new'或‘unchanged'。GTYPE 可以是 LTYPE 的选择
    或是‘changed'。
    GFMT (only) may contain:
      %<  lines from FILE1
      %>  lines from FILE2
      %=  lines common to FILE1 and FILE2
      %[-][WIDTH][.[PREC]]{doxX}LETTER  printf-style spec for LETTER
        LETTERs are as follows for new group, lower case for old group:
          F  first line number
          L  last line number
          N  number of lines = L-F+1
          E  F-1
          M  L+1
      %(A=B?T:E)  if A equals B then T else E
    LFMT (only) may contain:
      %L  contents of line
      %l  contents of line, excluding any trailing newline
      %[-][WIDTH][.[PREC]]{doxX}n  printf-style spec for input line number
    Both GFMT and LFMT may contain:
      %%  %
      %c'C'  the single character C
      %c'\OOO'  the character with octal code OOO
      C    the character C (other characters represent themselves)

  -d, --minimal            try hard to find a smaller set of changes
      --horizon-lines=NUM  keep NUM lines of the common prefix and suffix
      --speed-large-files  assume large files and many scattered small changes

      --help               display this help and exit
  -v, --version            output version information and exit

  FILES 的格式可以是‘FILE1 FILE2'、‘DIR1 DIR2'、‘DIR FILE...'或
      ‘FILE... DIR'。
If --from-file or --to-file is given, there are no restrictions on FILE(s).
如果 FILE 是‘-',则由标准输入读取内容。
如果输入相同,则退出状态为 0;1 表示输入不同;2 表示有错误产生。

以上是“linux中diff命令有什么用”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网操作系统频道!

--结束END--

本文标题: linux中diff命令有什么用

本文链接: https://www.lsjlt.com/news/256981.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • linux中diff命令有什么用
    这篇文章主要为大家展示了“linux中diff命令有什么用”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“linux中diff命令有什么用”这篇文章吧。这又是一个文件比较命令,熟练掌握会节省你很多...
    99+
    2023-06-09
  • Linux中diff命令怎么用
    这篇文章主要为大家展示了“Linux中diff命令怎么用”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Linux中diff命令怎么用”这篇文章吧。Linux常用命令diff命令 在最简单的情况下...
    99+
    2023-06-28
  • linux常用命令diff
    Linux diff (diff 即 different 简写)命令用于比较文件的差异。 diff 以逐行的方式,比较文本文件的异同处。如果指定要比较目录,则diff会比较目录中相同文件名的文件,但不会比较其中子目录 diff file_1...
    99+
    2023-09-12
    linux 运维 服务器
  • diff命令怎么在Linux中使用
    这期内容当中小编将会给大家带来有关diff命令怎么在Linux中使用,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。总览diff [选项] 源文件 目标文件描述在最简单的情况是, diff 比较两个文件的内...
    99+
    2023-06-12
  • Linux中Diff命令如何使用
    Linux中Diff命令如何使用,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。Linux下9种优秀的代码比对工具推荐这些比对工具其实都是基于 diff 命令,下面就详细介绍...
    99+
    2023-06-15
  • Linux常用命令diff怎么用
    这篇文章给大家分享的是有关Linux常用命令diff怎么用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。Linux常用命令diffstat命令用来显示diff命令输出信息的柱状图,用以显示diff命令比较两个文件...
    99+
    2023-06-28
  • Linux diff 命令的用法
    这篇文章主要介绍“Linux diff 命令的用法”,在日常操作中,相信很多人在Linux diff 命令的用法问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Linux diff 命令的用法”的疑惑有所帮助!...
    99+
    2023-06-16
  • 怎么在linux中使用diff命令
    今天就跟大家聊聊有关怎么在linux中使用diff命令,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。1.命令格式:diff[参数][文件1或目录1][文件2或目录2]2.命令功能:d...
    99+
    2023-06-13
  • diff与comm命令怎么在linux中使用
    diff与comm命令怎么在linux中使用?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。代码作用:比较两个UNIX文本文件,找出并打印文本2比文本1新增加的内容。代码调用...
    99+
    2023-06-13
  • Linux系统diff命令怎么用
    这篇文章主要介绍Linux系统diff命令怎么用,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!在Linux系统中diff命令主要用于比较文件差异,执行命令之后可以打印出文件之间的差异。它可以逐行地比较两个文件的内容,...
    99+
    2023-06-28
  • Linux命令中的cat命令有什么用
    今天给大家介绍一下Linux命令中的cat命令有什么用。文章的内容小编觉得不错,现在给大家分享一下,觉得有需要的朋友可以了解一下,希望对大家有所帮助,下面跟着小编的思路一起来阅读吧。cat命令的用途是连接文件或标准输入并打印。这个命令常用来...
    99+
    2023-06-28
  • Linux基础命令diff的用法
    这篇文章主要介绍“Linux基础命令diff的用法”,在日常操作中,相信很多人在Linux基础命令diff的用法问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Linux基础命令diff的用法”的疑惑有所帮助!...
    99+
    2023-06-06
  • docker diff命令怎么用
    这篇文章主要为大家展示了“docker diff命令怎么用”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“docker diff命令怎么用”这篇文章吧。docker diff当你运行了一个容器,但...
    99+
    2023-06-02
  • Linux中Openssl命令有什么用
    这篇文章主要介绍了Linux中Openssl命令有什么用,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。openssl命令的格式是"openssl command c...
    99+
    2023-06-09
  • Linux中host命令有什么用
    这篇文章给大家分享的是有关Linux中host命令有什么用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。Linux中的host命令应用举例一 命令语法功能:查出某个主机名的IPhost hostname [ser...
    99+
    2023-06-09
  • Linux 中wget命令有什么用
    这篇文章给大家分享的是有关Linux 中wget命令有什么用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。两个Linux命令wget和exec一个linux命令:wget ftp访问命令来下载文件的三种方式1)w...
    99+
    2023-06-09
  • linux中head命令有什么用
    这篇文章将为大家详细讲解有关linux中head命令有什么用,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。head 与 tail 就像它的名字一样的浅显易懂,它是用来显示开头或结尾某个数量的文字区块,he...
    99+
    2023-06-09
  • linux命令中pwd有什么用
    这篇文章主要介绍linux命令中pwd有什么用,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!pwd命令主要用于查看当前工作目录的完整路径。主要用法:pwd [option]常用范例:1.查看当前完整路径命令:pwd输...
    99+
    2023-06-09
  • Linux中Man命令有什么用
    小编给大家分享一下Linux中Man命令有什么用,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!Linux上命令非常多,不同的命令选项实现不同的功能,在日常的使用的...
    99+
    2023-06-15
  • Linux中touch命令有什么用
    这篇文章给大家分享的是有关Linux中touch命令有什么用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。touch命令可以用来修改文件的访问/修改时间戳。 更多的时候它会被用来快速创建一个空文件。touch命令...
    99+
    2023-06-16
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作