• linux shell 脚本攻略学习13--file命令详解,diff命令详解


    一、file命令详解

    find命令可以通过查看文件内容来找出特定类型的文件,在UNIX/ Linux系统中,文件类型并不是由文件扩展名来决定的(windows中却正是这么做的),file命令的目的是从多个文件中收集文件类型统计信息。输入 file --help查看帮助提示信息,如下:

    amosli@amosli-pc:~$ file --help
    Usage: file [OPTION...] [FILE...]
    Determine type of FILEs.
    
          --help                 display this help and exit
      -v, --version              output version information and exit
      -m, --magic-file LIST      use LIST as a colon-separated list of magic
                                   number files
      -z, --uncompress           try to look inside compressed files
      -b, --brief                do not prepend filenames to output lines
      -c, --checking-printout    print the parsed form of the magic file, use in
                                   conjunction with -m to debug a new magic file
                                   before installing it
      -e, --exclude TEST         exclude TEST from the list of test to be
                                   performed for file. Valid tests are:
                                   ascii, apptype, compress, elf, soft, tar, tokens, troff
      -f, --files-from FILE      read the filenames to be examined from FILE
      -F, --separator STRING     use string as separator instead of `:'
      -i, --mime                 output MIME type strings (--mime-type and
                                   --mime-encoding)
          --apple                output the Apple CREATOR/TYPE
          --mime-type            output the MIME type
          --mime-encoding        output the MIME encoding
      -k, --keep-going           don't stop at the first match
      -l, --list                 list magic strength
      -L, --dereference          follow symlinks (default)
      -h, --no-dereference       don't follow symlinks
      -n, --no-buffer            do not buffer output
      -N, --no-pad               do not pad output
      -0, --print0               terminate filenames with ASCII NUL
      -p, --preserve-date        preserve access times on files
      -r, --raw                  don't translate unprintable chars to ooo
      -s, --special-files        treat special (block/char devices) files as
                                 ordinary ones
      -C, --compile              compile file specified by -m
      -d, --debug                print debugging messages

    语法:

    file [OPTION...] [FILE...]
    
    file[必要参数][选择参数][文件或者目录]

    必要参数

    -b 文件名不显示
    -c 显示详细处理信息
    -L 操作对象为符号链接所指向的对象
    -z 压缩文件的处理
    -C 列出magic.mgc的输出
    -n 将结果输出到标准输出
    -f<文件名> 指定文件名
    -m<文件名> 指定魔法数字名

    选择参数

    --help 显示帮助信息
     -v 显示版本信息

    所以加上参数的语法也可以改写为:

    file [-beLvz][-f <名称文件>][-m <魔法数字文件>...][文件或目录...]

    实例:

    1.查看所有文件类型 file *

    amosli@amosli-pc:~$ file *
    bank:                   directory
    c:javagc.log:           empty
    data.txt:               ASCII text
    da.txt:                 ASCII text
    ddd:                    UTF-8 Unicode text, with very long lines
    eclipse222:             setgid directory
    examples.desktop:       UTF-8 Unicode text
    home:                   Python script, UTF-8 Unicode text executable
    learn:                  directory
    Public:                 directory
    scriptfifo:             fifo (named pipe)
    socialcredit_metrix.py: Python script, UTF-8 Unicode text executable
    sorted.txt:             ASCII text
    temp.png:               PNG image data, 1218 x 717, 8-bit/color RGBA, non-interlaced
    tmp.zip:                Zip archive data, at least v2.0 to extract

    第一列为文件名称,第二列为文件类型。

    2.查看具体某一文件的类型:file filename

    
    amosli@amosli-pc:~$ file data.txt 
    data.txt: ASCII text

    3.-b参数,打印出不包含文件名的文件类型信息:

    amosli@amosli-pc:~$ file -b data.txt 
    ASCII text

    4.-z 参数,可以获取用gzip、zip压缩过的文件的类型

    amosli@amosli-pc:~$ file -z tmp.zip 
    tmp.zip: empty (Zip archive data, at least v2.0 to extract)

    5.-n参数,将信息标准输出

    amosli@amosli-pc:~/learn/re$ ls
    a.txt
    amosli@amosli-pc:~/learn/re$ file -n a.txt  > b
    amosli@amosli-pc:~/learn/re$ ls
    a.txt  b

    二、diff 命令详解

    在日常工作中有时候想要比较两个文件的差异,一般来说没有什么比较好的办法,之前尝过md5sum和sha1可以检验文件的md5值是否一致,但想了解文件的内容具体有哪些差异就比较困难了,下面开始介绍diff命令来帮助解决这一难题:

    diff --help看一下提示,由于提示信息相当多,非常占篇幅这里就不贴出来了。

    语法:

    diff [OPTION]... FILES
    
    diff[参数][文件1或目录1][文件2或目录2]

    参数:

    -  指定要显示多少行的文本。此参数必须与-c或-u参数一并使用。
     -a或--text  diff预设只会逐行比较文本文件。
    -b或--ignore-space-change  不检查空格字符的不同。
    -B或--ignore-blank-lines  不检查空白行。
    -c  显示全部内文,并标出不同之处。
    -C或--context  与执行"-c-"指令相同。
    -d或--minimal  使用不同的演算法,以较小的单位来做比较。
    -D或ifdef  此参数的输出格式可用于前置处理器巨集。
    -e或--ed  此参数的输出格式可用于ed的script文件。
    -f或-forward-ed  输出的格式类似ed的script文件,但按照原来文件的顺序来显示不同处。
    -H或--speed-large-files  比较大文件时,可加快速度。
    -l或--ignore-matching-lines  若两个文件在某几行有所不同,而这几行同时都包含了选项中指定的字符或字符串,则不显示这两个文件的差异。
    -i或--ignore-case  不检查大小写的不同。
    -l或--paginate  将结果交由pr程序来分页。
    -n或--rcs  将比较结果以RCS的格式来显示。
    -N或--new-file  在比较目录时,若文件A仅出现在某个目录中,预设会显示:Only in目录:文件A若使用-N参数,则diff会将文件A与一个空白的文件比较。
    -p  若比较的文件为C语言的程序码文件时,显示差异所在的函数名称。
    -P或--unidirectional-new-file  与-N类似,但只有当第二个目录包含了一个第一个目录所没有的文件时,才会将这个文件与空白的文件做比较。
    -q或--brief  仅显示有无差异,不显示详细的信息。
    -r或--recursive  比较子目录中的文件。
    -s或--report-identical-files  若没有发现任何差异,仍然显示信息。
    -S或--starting-file  在比较目录时,从指定的文件开始比较。
    -t或--expand-tabs  在输出时,将tab字符展开。
    -T或--initial-tab  在每行前面加上tab字符以便对齐。
    -u,-U或--unified=  以合并的方式来显示文件内容的不同。
    -v或--version  显示版本信息。
    -w或--ignore-all-space  忽略全部的空格字符。
    -W或--width  在使用-y参数时,指定栏宽。
    -x或--exclude  不比较选项中所指定的文件或目录。
    -X或--exclude-from  您可以将文件或目录类型存成文本文件,然后在=中指定此文本文件。
    -y或--side-by-side  以并列的方式显示文件的异同之处。
    --help  显示帮助。
    --left-column  在使用-y参数时,若两个文件某一行内容相同,则仅在左侧的栏位显示该行内容。
    --suppress-common-lines  在使用-y参数时,仅显示不同之处。

    实例:

    1、比较两个txt文件,version1.txt,version2.txt

    amosli@amosli-pc:~/learn/re$ cat version1.txt version2.txt  -n
         1    this is test #version1.txt
         2    line2
         3    line3
         4    line4
         5    this is endding
    6 this is test #version2.txt 7 line2 8 line4 9 this is endding 10 test over
    amosli@amosli-pc:~/learn/re$ diff version1.txt version2.txt 
    3d2
    < line3
    5a5
    > test over

    上面的输出是非一体化的(non-unified)形式的输出。

    2.那么在使用-u参数的情况下一体化形式的输出如下:

    amosli@amosli-pc:~/learn/re$ diff  -u version1.txt version2.txt 
    --- version1.txt    2013-12-28 01:35:50.624355632 +0800
    +++ version2.txt    2013-12-28 01:37:35.736876852 +0800
    @@ -1,5 +1,5 @@
     this is test
     line2
    -line3
     line4
     this is endding
    +test over

    -u参数用于生成一体化的输出,因为一体化的输出更利于阅读,所以一般情况下-u参数使用频率较高.

    -u参数的英文提示如下:

      -u, -U NUM, --unified[=NUM]   output NUM (default 3) lines of unified context

    3.有没有更利于阅读的方式了呢?-y参数很明显要好于以上两者

    amosli@amosli-pc:~/learn/re$ diff  -y version1.txt version2.txt 
    this is test                            this is test
    line2                                line2
    line3                                  <
    line4                                line4
    this is endding                            this is endding
                                      >    test over

    -y参数的英文提示信息如下:

      -y, --side-by-side            output in two columns

    分为两列进行显示,对不同之处进行标记,相当方便。

     说明:

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

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

     4.将两个文件的不同之处重定向输出到一个文件

    amosli@amosli-pc:~/learn/re$ diff -u version1.txt version2.txt > version.patch
    amosli@amosli-pc:~/learn/re$ cat version.patch 
    --- version1.txt    2013-12-28 01:35:50.624355632 +0800
    +++ version2.txt    2013-12-28 01:37:35.736876852 +0800
    @@ -1,5 +1,5 @@
     this is test
     line2
    -line3
     line4
     this is endding
    +test over

    5.对文件version1.txt进行修改使其和version2.txt一样

    amosli@amosli-pc:~/learn/re$ patch -p1 version1.txt < version.patch 
    patching file version1.txt
    amosli@amosli-pc:~/learn/re$ cat version1.txt 
    this is test
    line2
    line4
    this is endding
    test over
    amosli@amosli-pc:~/learn/re$ cat version2.txt 
    this is test
    line2
    line4
    this is endding
    test over

    6.目录之间的比较

    语法/参数:

    diff -Naur directory1 directory2
    -N 所有缺失的文件作为空文件
    -a 所有文件视为文本文件
    -u 一体化输出
    -r 递归遍历

    例:

    创建不同的子目录进行比较:

    amosli@amosli-pc:~/learn/re$ mkdir -p dd/db
    amosli@amosli-pc:~/learn/re$ mkdir -p db/dc
    amosli@amosli-pc:~/learn/re$ diff dd db
    Only in dd: db
    Only in db: dc

    创建子目录相同的目录进行比较:

    amosli@amosli-pc:~/learn/re$ mkdir -p d1/d2/d3
    amosli@amosli-pc:~/learn/re$ mkdir -p d2/d2/d3
    amosli@amosli-pc:~/learn/re$ diff  -u d1 d2
    Common subdirectories: d1/d2 and d2/d2
  • 相关阅读:
    Maven项目类型和JAVASE项目和JAVAEE项目的关系
    使用faker 生成测试数据
    python 面向对象
    python csv读写
    分治
    django 部署
    js 时间格式转换
    python环境
    枚举
    递归
  • 原文地址:https://www.cnblogs.com/amosli/p/3494985.html
Copyright © 2020-2023  润新知