Discover Centmin Mod today
Register Now

Get difference in number value

Discussion in 'System Administration' started by Jon Snow, Nov 14, 2022.

  1. Jon Snow

    Jon Snow Active Member

    680
    130
    43
    Jun 30, 2017
    Ratings:
    +192
    Local Time:
    8:42 AM
    Nginx 1.13.9
    MariaDB 10.1.31
    I have two folders with the same file names.


    Folder A has 200,000 files.
    Folder B has 220,000 files (200,000 copied from Folder A and 20,000 new file names).

    Is there a command I can run to get the difference between these two folders (answer for this example = 20,000) in a number value? I don't want to list out the differences otherwise I'd use the DIFF command.
     
  2. eva2000

    eva2000 Administrator Staff Member

    49,587
    11,382
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +17,686
    Local Time:
    10:42 PM
    Nginx 1.21.x
    MariaDB 10.x
    Just count each folder and subtract them i.e. 2 folders diffcount1 and diffcount2 have 1 and 2 files respectively
    Code (Text):
    ls diffcount1 | wc -l
    1
    
    ls diffcount2 | wc -l
    2
    

    Then mathematically subtract the counts i.e. 2-1 = 1
    Code (Text):
    echo $(($(ls diffcount2 | wc -l) - $(ls diffcount1 | wc -l)))
    1
    
     
  3. Jon Snow

    Jon Snow Active Member

    680
    130
    43
    Jun 30, 2017
    Ratings:
    +192
    Local Time:
    8:42 AM
    Nginx 1.13.9
    MariaDB 10.1.31
    I might have worded this poorly, sorry.

    I can already get the number of files from each folder easily and work out the math and get a difference. What I want to do is find the number of different files between Folder A and Folder B.

    So Folder A has 20 photos.
    Folder B has 5 photos with the same file names from Folder A.

    The command will answer with 15 because Folder A has 15 files with different names than Folder B. This would be incorrect if they have the similar number of files but with completely different file names in each.

    So it's about matching file names and getting the difference.

    It shouldn't work if Folder A and Folder B have completely different contents in terms of file name.
     
  4. Jon Snow

    Jon Snow Active Member

    680
    130
    43
    Jun 30, 2017
    Ratings:
    +192
    Local Time:
    8:42 AM
    Nginx 1.13.9
    MariaDB 10.1.31
    I can list out the different file names with the DIFF command, but it wouldn't give me a #. The folders have too many different files to list them out in a single command.
     
  5. eva2000

    eva2000 Administrator Staff Member

    49,587
    11,382
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +17,686
    Local Time:
    10:42 PM
    Nginx 1.21.x
    MariaDB 10.x
    Not entirely sure what you want but if it's counting diff output just grep count it

    directory diffcount2 has 2 files that differ from directory diffcount1
    Code (Text):
    diff -q diffcount1 diffcount2
    Only in diffcount2: file2.txt
    Only in diffcount2: file3.txt
    

    grep count matching diffcount2 name of directory from output for 'Only in diffcount2'
    Code (Text):
    diff -q diffcount1 diffcount2 | grep -c 'diffcount2'
    2
    

    If you need to match and count files in diffcount1 directory, just change the grep count term
    Code (Text):
    diff -q diffcount1 diffcount2 | grep -c 'diffcount1'
     
  6. Jon Snow

    Jon Snow Active Member

    680
    130
    43
    Jun 30, 2017
    Ratings:
    +192
    Local Time:
    8:42 AM
    Nginx 1.13.9
    MariaDB 10.1.31
    This looks like what I need. Thanks!