4

When comparing 2 files using diff, the original file is provided as first argument, and the new (modified) as second argument.

diff original new

Presumably, the logic behind this ordering was to show how to transform the original into the new one, using left-to-right convention for the flow of time.

I am having problems with this logic. When I am diffing two files, I intuitively use the inverse notation:

diff new original

so that I can, after reviewing the changes, cat the new file into the old one, and thus "patch" it to the new version.

cat new > original

But using "my" inverted notation is confusing, because diff shows + for deletion, and - for addition.

How can I resolve this problem?

closed as primarily opinion-based by Kusalananda, RalfFriedl, n.st, Isaac, mosvy Nov 25 '18 at 21:30

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

  • FWIW I also find diff reads backwards but I've not really found a useful solution. I suppose I could trivially create my own variant diffr but I've never quite got round to it. – roaima Oct 31 '16 at 22:04
  • 3
    Your argument order doesn't make sense to me, but if to implement it I would also do some trivial script or alias. Apart from that, "cp" would probably be more intuitive than "cat" to copy files afterwards. – Göran Uddeborg Oct 31 '16 at 22:12
  • @GöranUddeborg, cp new original is still the reverse order to the diff command. I would suggest just using cat >original <new if you want something where the sequence matches. And double check your commands before you run them. ;) – Wildcard Nov 1 '16 at 0:41
  • 4
    You know, people had this argument in 1970's, when they were trying to decide if assignment in assembly languages should be written as MOV src,dest or MOV dest,src. There is still no universally accepted convention to this day. Other debates you might also consider investigating: vi vs. emacs, tabs vs. spaces. – Satō Katsura Nov 1 '16 at 7:41
  • How can I resolve this problem? Which problem? That you are using a tool contrary to how it was designed to work? That the result doesn't match what you want? or That you are getting confused?. Possible solutions: (1)develop a new tool. (2) use sed/awk/tr to change the + to - and viceversa. (3) Change the way you read the output to understand it the opposite way (4) make a function that takes the arguments to cat the other way around, like mycat(){ cp "$2" "$1"; } or (In MY opinion:) (5) Use the tool as it was designed to work and get used to it. Or even (6) make a function for diff. – Isaac Nov 26 '18 at 0:15
1

There is a simple solution!

Write the following script:

if [ "$#" -ne 2 ]; then
    echo "USAGE: diff2 newfile oldfile"
    exit 1;
fi

diff $2 $1

Save it on one of the folders on PATH (maybe /bin or /usr/bin) as diff2.

And that's it, you can now call:

diff2 newfile oldfile, which will then call diff oldfile newfile

1

A quick look at the man page suggests you can use the --from-file=original and --to-file=new options, if you want to be explicit. You'll be typing a little more, but will give you the flexibility you need and as a bonus make things explicit.

diff --to-file=new --from-file=original

If you want, you can also write a simple wrapper script this that takes only the file names in the order you want and runs this for you.

EDIT: This is for GNU diff...not sure if this are standard (POSIX) options though.

  • --from-file=FILE1 Compare FILE1 to each operand; FILE1 may be a directory. --to-file=FILE1 Compare each operand to FILE1; FILE1 may be a directory. Source. Specifying both doesn't make any sense and results in an error message for me. – asynts May 10 at 10:47

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