0

This question is similar to How to diff only the first line of two files?, but recursively. I would like to diff two directories but:

  • diff only the first N lines of each file in the two directories
  • diff everything except the first N lines of each file in the two directories
  • Normally you can compare using diff -r dir1 dir2. You may also play with -I RE option to ignore certain matching lines. – kenorb Oct 4 '15 at 16:35
  • Why you want to ignore first n lines? What's your motivation/scenario? – kenorb Oct 4 '15 at 16:35
  • Because they contain a unimportant metadata, such as time, date, title which I am not interested in, instead I only want to compare the data of the files. – lanoxx Oct 5 '15 at 9:21
1

My first suggestion for you is to use Meld. It works from the command-line as well.

It has the following features which can interest you:

  • Compare two or three directories file-by-file, showing new, missing, and altered files.
  • Use the built-in regex text filtering to ignore uninteresting differences.

The only thing which you need to do is to figure out the right regex patterns which can be used to ignore the unimportant data (depending on the syntax of your metadata which you want to ignore).

0

These two loops use diff -qr to do an initial diff, principally to get the filenames easily, then do an individual diff on the found pair of files. sed is used to clear the first N lines, or to keep only the first N lines. First to clear N lines:

N=2
diff -qr dir1 dir2 |
grep '^Files.*differ' |
while read x a x b x
do  diff --label "$a" --label "$b" -u <(sed "1,${N}s/.*//" <"$a") <(sed "1,${N}s/.*//" <"$b")
done

And this to keep N:

diff -qr dir1 dir2 |
grep '^Files.*differ' |
while read x a x b x
do  diff --label "$a" --label "$b" -u <(sed -n "1,${N}p" <"$a") <(sed -n "1,${N}p" <"$b")
done

This assumes no spaces/tabs in filenames.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.