1

My scenario is to perform side by side diff directories using:

diff -ry <folder1> <folder2>

along with the line numbers in the diff output. By default line numbers are not displayed in the side by side diff and the parameter --new-line-format doesn't work along with diff -y only works with diff -u.

What i have tried is to do [For files only]:

diff -y <(cat -n file1) <(cat -n file2)

to generate line numbers. The above command first generates line numbers and then passes it to diff command, so the line numbers are kept intact in the diff result.

But when it comes to use diff -ry, i am unable to do it. Is there any way to apply cat -n using something like xargs [like a preprocessor] in the diff -ry command ?

  • 1
    This is a re-iteration of a previous post. It didn't get any more meaningful in the mean time, I'm afraid. – Satō Katsura Sep 3 '16 at 9:14
1

You can convert inline diff -ru output into a side-by-side presentation, with line numbers and colour using the python program cdiff also known as cdiff.py (not to be confused with the cdiff wrapper that is part of colordiff), by Matthew Wang.

Follow any of the suggested installation methods given in the link, or just extract the cdiff.py script from the download into your PATH, then you can do

diff -ru dir1 dir2 | cdiff.py -s
  • Thanks for the quick response. Your solution is good but cdiff shows some unwanted information like --- ./myfolder1/subfolder2/myfile.txt 2016-03-30 13:10:25.403649965 -0400 +++ ./myfolder2/subfolder2/myfile.txt 2016-08-14 10:16:08.590097212 -0400 which i don't want. Secondly, it would be nice if i can use any native command for the cause. – Ronit Mishra Sep 3 '16 at 14:47
1

Stephen Harris has given a very good answer to the previous version of this question.  Unfortunately, it cannot handle the pathological case of filenames with newlines in them.  Here is a minor adaptation of his answer that handles that case.  (I've also changed it to use cat -n instead of nl, and to use the -y option to diff.)

find folder1 -type f -exec bash -c 'for file1
do
  file2="${file1/folder1/folder2}"
  echo "Comparing $file1 to $file2"
  diff -y <(cat -n "$file1") <(cat -n "$file2")
done' bash {} +

Since you asked for an xargs solution in a comment on Stephen’s answer, here you go:

find folder1 -type f -print0 | xargs -0 bash -c 'for file1
do
  file2="${file1/folder1/folder2}"
  echo "Comparing $file1 to $file2"
  diff -y <(cat -n "$file1") <(cat -n "$file2")
done' bash

Note that these commands must be done in bash, because the POSIX shell doesn’t support ${parameter/pattern/string}, and the xargs solution must be done in GNU-land, because POSIX doesn’t support find -print0 or xargs -0.  (Of course, you could replace -print0 with -print and drop the -0, but then you’d lose the capability to handle filenames with newlines in them.)

  • The above solutions will fail miserably because the above technique is unable to find the files which are only in $file2. The above command traverses through the subfolders which contain only in $file1. What if there are some new files in $file2 and not in $file1 ? – Ronit Mishra Sep 3 '16 at 14:50
  • 1
    What if there are some files in folder2 and not in folder1? What do you want done with them? Your previous question (of which this is a rewrite) said something about comparing same to same, so folder1/alpha should be compared to folder2/alpha, folder1/beta should be compared to folder2/beta, etc. You have never said what to do if folder1/gamma exists and folder2/gamma doesn't, or folder2/iota exists and folder1/iota doesn't, etc. Please don't say that my answer "will fail miserably" because it fails to satisfy a requirement that you didn't state. I can't read your mind. – G-Man Sep 4 '16 at 23:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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