Using diff to compare directories & files

Discussion in 'macOS Mojave (10.14)' started by Tech198, May 18, 2019.

  1. Tech198 macrumors G5

    Joined:
    Mar 21, 2011
    Location:
    Australia, Perth
    #1
    I'm trying to tell what diff does in Terminal, but all i can find is it compares directories and/or files..

    What i'm interested to know is "how" it compares ? File size, AND time stamp (can be excluded some of these) ?

    I'm only interested in filenames and filsizes only,, not time stamp info. so i'm not sure if diff would still flag inaccurate creation.. or weather this can be exuded....

    I've already come down to using diff -r and optional q on main folder will scan ALL sub directories within, which is exactly what i'm wanting, but there is no point in using the command if you can't exclude bits and pieces..
     
  2. MisterSavage macrumors 6502a

    MisterSavage

    Joined:
    Nov 10, 2018
    #2
    I use diff to compare the contents of different text files. I don't think that timestamp is a factor.

    To test I create file1 and file2 that contain the same text but have different creation times. A 'diff file1 file2' returns nothing, which is what I expected.
     
  3. haralds macrumors 6502a

    haralds

    Joined:
    Jan 3, 2014
    Location:
    Silicon Valley, CA
    #3
    $ man diff

    NAME

    diff - compare files line by line


    SYNOPSIS

    diff [OPTION]... FILES


    DESCRIPTION

    Compare files line by line.
    ...
     
  4. Tech198 thread starter macrumors G5

    Joined:
    Mar 21, 2011
    Location:
    Australia, Perth
    #4
    Yes, i already looked at the flags.

    I did a test as well and it looks like it only picks uo different based on file name only... not size..

    To test, i created two directory, with same text file name in each "Untitled"

    I then edited one of then adding more text,to increase bytes size... but kept same file name..

    I then ran diff -r passing two directories in Terminal and it didn't come out with any differences. If dff cannot compare file sizes as well as filename differences then its not gonna work for me.

    As for comparison, i understood diff would out differences in filename AND file size.. otherwise i would either need to resort to utilities, or just go into each and every folder which takes time..

    Ideally i'm using diff on main dir in two different locations, so it will go into all sub-folders 3 or more levels deep

    Perhaps diff is not the best use case for this ?
     
  5. MisterSavage macrumors 6502a

    MisterSavage

    Joined:
    Nov 10, 2018
    #5
    Was your test something like this? It's working for me.

    $ diff -r one/one.txt two/one.txt

    2a3,4

    >

    > new lines.
     
  6. NoBoMac macrumors 68020

    NoBoMac

    Joined:
    Jul 1, 2014
    #6
    This^^^.

    Also working here.

    Code:
    >> mkdir f1
    >> echo "this is text" > f1/f1.txt
    >> echo "this is file 2" > f1/f2.txt
    >> ls f1
    f1.txt    f2.txt
    >> cp -r f1 f2
    >> ls f2
    f1.txt    f2.txt
    >> diff -r f1 f2
    >> echo "new text" > f2/f2.txt
    >> echo "new file" > f2/f3.txt
    >> ls f2
    f1.txt    f2.txt    f3.txt
    >> diff -r f1 f2
    diff -r f1/f2.txt f2/f2.txt
    1c1
    < this is file 2
    ---
    > new text
    Only in f2: f3.txt
    >> diff -rq f1 f2
    Files f1/f2.txt and f2/f2.txt differ
    Only in f2: f3.txt
    >> diff -r -q f1 f2
    Files f1/f2.txt and f2/f2.txt differ
    Only in f2: f3.txt
    >>
    
     
  7. Tech198 thread starter macrumors G5

    Joined:
    Mar 21, 2011
    Location:
    Australia, Perth

Share This Page

6 May 18, 2019