Say I have a file foo and a file bar. I now call the following commands:
$ diff foo bar > diff_foobar
$ rm bar
Is there a(n easy) way to programmatically reconstruct the missing bar from foo and diff_foobar?
Reconstructing files from diffs, also known as "applying diffs", is what
utility patch does:
## Create two arbitrary files, `original' and `altered':
$ seq 0 3 >original
$ seq 7 11 >altered
## Compute a diff from `original' to `altered':
$ diff original altered >diff
## Copy `original' before patching it
$ cp -vi original original.0
`original' -> `original.0'
$ patch <diff original # This modifies `original'!
patching file original
## Confirm that we rebuilt the file from `original' and `diff`
$ diff --report-identical-files original altered
Files original and altered are identical
The patch command also has a bunch of options, in particular to apply patches in reverse.
diff has two output modes, controlled by options: human readable and machine readable, e.g. patch.
Yes, you apply the patch:
You did:
$ diff foo bar > diff_foobar
$ rm bar
Now do:
$ patch foo -o bar < diff_foobar