Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Diff files in two folders ignoring the first line

Tags:

bash

unix

diff

gnu

I have two folders of files that I want to diff, except I want to ignore the first line in all the files. I tried

  diff -Nr <(tail -n +1 folder1/) <(tail -n +1 folder2/) 

but that clearly isn't the right way.

like image 388
Billy Fung Avatar asked Oct 15 '25 07:10

Billy Fung


1 Answers

If the first lines that you want to ignore have a distinctive format that can be matched by a POSIX regular expression, then you can use diff's --ignore-matching-lines=... option to tell it to ignore those lines.

Failing that, the approach you want to take probably depends on your exact requirements. You say you "want to diff" the files, but it's not obvious exactly how faithfully your resulting output needs to match what you would get from diff -Nr if it supported that feature. (For example, do you need the line numbers in the diff to correctly identify the line numbers in the original files?)

The most precisely faithful approach would probably be as follows:

  • Copy each directory to a fresh location, using cp --recursive ....
  • Edit the first line of each file to prepend a magic string like IGNORE_THIS_LINE::, using something like find -type f -exec sed -i '1 s/^/IGNORE_THIS_LINE::/' '{}' ';'.
  • Use diff -Nr --ignore-matching-lines=^IGNORE_THIS_LINE:: ... to compare the results.
    • Pipe the output to sed s/IGNORE_THIS_LINE:://, so as to filter out any occurrences of IGNORE_THIS_LINE:: that still show up (due to being within a few lines of non-ignored differences).
like image 122
ruakh Avatar answered Oct 17 '25 15:10

ruakh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!