How to make diff print a full file diff when a file does not exist?

With at least some versions of GNU diffutils' diff, you can use the -N flag to do that directly (although the documentation appears to say that only works for directory diffs).

$ ls
foo
$ cat foo
foo
$ diff -u foo bar
diff: bar: No such file or directory
$ diff -uN bar foo
--- bar 1970-01-01 01:00:00.000000000 +0100
+++ foo 2012-09-04 12:45:34.000000000 +0200
@@ -0,0 +1 @@
+foo
$ diff -uN foo bar
--- foo 2012-09-04 12:45:34.000000000 +0200
+++ bar 1970-01-01 01:00:00.000000000 +0100
@@ -1 +0,0 @@
-foo
$ diff -v
diff (GNU diffutils) 2.8.1
Copyright (C) 2002 Free Software Foundation, Inc.

have you tried diff -u /dev/null filename ?

that worked for me when i just tried it.

alternatively:

touch missing
diff -u missing filename
rm missing

Also, if you diff directories (e.g. diff -x /path/to/backup -uN / /path/to/backup) then diff will recursively show full diffs of all files, including missing files. You need the -x, --exclude=PAT option because /path/to/backup is a subdir of /

(thanks to Mat for reminding me of the -N option)

Tags:

Diff