Linux‎ > ‎

File different tools on linux, diff and others in diffutils and diffstat

Thank you for visiting this page, this page has been update in another link File compare tools on linux
diffutils includs not just diff, also other following tools.
/usr/bin/cmp
/usr/bin/diff
/usr/bin/diff3
/usr/bin/sdiff
While, diffstat package only has diffstat one tool.
Tools from diffutils have similar usage, so let's start with diff
SYNOPSIS
       diff [OPTION]... FILES

Example #1, no option
 $diff mtx.c mtx-driveinfo.c
124c124
< struct command_table_struct
---
> /*struct command_table_struct
155a156,166
> }; -- commented off on Mar. 14 2008*/
>
> struct command_table_struct {
>   int num_args;
>   char *name;
>   void (*command)(void);
>   int need_device;
>   int need_status;
> } command_table[] = {
>   { 0, "status", Status, 1,1 },
>   { 0, "--version", Version, 0,0 }
158c169
< static void Usage()
---
> /*static void Usage()
181c192,198
< }
---
> } --commented off by Simon on Mar.14 2008*/
>
...
Example #2, option -p --show-c-function
              Show which C function each change is in.
$diff -p mtx.c mtx-driveinfo.c
*** mtx.c    2012-07-25 09:48:40.000000000 -0700
--- mtx-driveinfo.c    2012-07-25 07:47:44.000000000 -0700
*************** static void Invert2(void);
*** 121,127 ****
  static void Exchange(void);
  static void AltReadElementStatus(void);
 
! struct command_table_struct
  {
      int num_args;
      char *name;
--- 121,127 ----
  static void Exchange(void);
  static void AltReadElementStatus(void);
 
! /*struct command_table_struct
  {
      int num_args;
      char *name;
*************** command_table[] =
*** 153,161 ****
      { 3, "exchange", Exchange, 1, 1 },
      { 0, "altres", AltReadElementStatus, 0,0},
      { 0, NULL, NULL }
  };
...
Example #3, option -q and -r , useful when you want to compare directories
       -q  --brief
              Output only whether files differ.
       -r  --recursive
              Recursively compare any subdirectories found.
I copied whole mtx-1.3.12 whole directory to mtx_tmp, then changed two files, here is use case for option -q and -r, it tells me which files has been changed.
$diff -q -r ./mtx-1.3.12 ./mtx_tmp/mtx-1.3.12
Files ./mtx-1.3.12/mtx.c and ./mtx_tmp/mtx-1.3.12/mtx.c differ
Files ./mtx-1.3.12/mtx-inventory.c and ./mtx_tmp/mtx-1.3.12/mtx-inventory.c differ
Example #4. option -y, not good for big files, but useful for small files.
       -y  --side-by-side
              Output in two columns.


static void Usage()                          |    /*static void Usage()
{                                {
    fprintf(stderr, "Usage:\n\                    fprintf(stderr, "Usage:\n\
    mtx --version\n\                        mtx --version\n\
    mtx [ -f <loader-dev> ] noattach <more commands>\n\        mtx [ -f <loader-dev> ] noattach <more commands>\n\
    mtx [ -f <loader-dev> ] inquiry | inventory \n\            mtx [ -f <loader-dev> ] inquiry | inventory \n\
    mtx [ -f <loader-dev> ] [altres] [nobarcode] status\n        mtx [ -f <loader-dev> ] [altres] [nobarcode] status\n
...
    sys$exit(VMS_ExitCode);                        sys$exit(VMS_ExitCode);
#endif                                #endif
}                                  |    } --commented off on Mar.14 2008*/
                                  >
                                  >    static void Usage()
                                  >    {
                                  >      fprintf(stderr, "Usage:\n\
                                  >      mtx-driveinfo --version\n\
                                  >      mtx-driveinfo [ -f <loader-dev> ] [nobarcode] status\n");

                                  >    #ifndef VMS
                                  >      exit(1);

Example #5,        --left-column
              Output only the left column of common lines.
$diff -y --left-column mtx.c mtx-driveinfo.c
...
                                                             >      int need_status;
                                                             >    } command_table[] = {
                                                             >      { 0, "status", Status, 1,1 },
                                                             >      { 0, "--version", Version, 0,0 }
};                                                           (
                                                             (
static void Usage()                                          |    /*static void Usage()
{                                                            (
    fprintf(stderr, "Usage:\n\                               (
    mtx --version\n\                                         (
    mtx [ -f <loader-dev> ] noattach <more commands>\n\      (
    mtx [ -f <loader-dev> ] inquiry | inventory \n\          (
    mtx [ -f <loader-dev> ] [altres] [nobarcode] status\n    (
    mtx [ -f <loader-dev> ] [altres] first [<drive#>]\n\     (
    mtx [ -f <loader-dev> ] [altres] last [<drive#>]\n\      (
...
Example #6, sdiff
NAME
       sdiff - side-by-side merge of file differences
It's more likely is a diff embeded with option '-y', see this
$sdiff mtx.c mtx-driveinfo.c --diff-program=cmp
cmp: invalid option -- 'y'
cmp: Try `cmp --help' for more information.
Example #7, diff3, similar with sdiff, it uses diff internally when comparing, you can specify other program with option --diff-program
NAME
       diff3 - compare three files line by line

SYNOPSIS
       diff3 [OPTION]... MYFILE OLDFILE YOURFILE

$cat a
aaaa
bbbb
$cat b
dddd
$cat c
aaaa

$diff3 a b c
====
1:1,2c
  aaaa
  bbbb
2:1c
  dddd
3:1c
  aaaa
  • The very first line ‘====’ signifies that all the three files differ.
  • Next, the line ’1:1,2c’ means that for the first (1:) file given as argument to diff3 (a in our case), lines in the range of one to two (1,2) are different when compared with the other two files and need to be changed(c) and these two lines are also shown in the next two lines of the output.
  • Similarly ’2:1c’ and ’3:1c’ can be comprehended.
Example #8 diffstat, useful to have a overall view how much changes between files.
NAME
       diffstat - make histogram from diff-output

SYNOPSIS
       diffstat [options] [file-specifications]

$diff mtx.c mtx-driveinfo.c | diffstat
 unknown |   35 +++++++++++++++++++++++++++++++----
 1 file changed, 31 insertions(+), 4 deletions(-)

Example #9 cmp,
NAME
       cmp - compare two files byte by byte

SYNOPSIS
       cmp [OPTION]... FILE1 [FILE2 [SKIP1 [SKIP2]]]
       -b  --print-bytes
              Print differing bytes.

       -i SKIP  --ignore-initial=SKIP
              Skip the first SKIP bytes of input.

       -i SKIP1:SKIP2  --ignore-initial=SKIP1:SKIP2

              Skip the first SKIP1 bytes of FILE1 and the first SKIP2 bytes of FILE2.

       -l  --verbose
              Output byte numbers and values of all differing bytes.

       -n LIMIT  --bytes=LIMIT
              Compare at most LIMIT bytes.

       -s  --quiet  --silent
              Output nothing; yield exit status only.

       -v  --version
              Output version info.

       --help Output this help.

To people used to use diff, the most of useful case is option '-s'
$cmp  mtx.c mtx-driveinfo.c
mtx.c mtx-driveinfo.c differ: byte 3768, line 124

$cmp -b mtx.c mtx-inventory.c
mtx.c mtx-inventory.c differ: byte 1499, line 40 is  56 .  55 -

$cmp -s mtx.c mtx-inventory.c

$echo $?
1



Comments