Let diff create new files, but not remove existing files

diff()patch

Maybe I am missing something, but I'd like a diff between two directories to create files that don't exist, but not remove files that do. So, given something like:

./a/foo
./b/bar

This command:

diff -urN a b

Will show:

diff -urN a/bar b/bar
--- a/bar       1969-12-31 17:00:00.000000000 -0700
+++ b/bar       2012-05-22 10:09:05.221356000 -0600 @@ -0,0 +1 @@
+bar 
diff -urN a/foo b/foo
--- a/foo       2012-05-22 10:08:54.133138000 -0600
+++ b/foo       1969-12-31 17:00:00.000000000 -0700 @@ -1 +0,0 @@
-Foo

What I'd like is essentially this:

diff -urN a/bar b/bar
--- a/bar       1969-12-31 17:00:00.000000000 -0700
+++ b/bar       2012-05-22 10:09:05.221356000 -0600 @@ -0,0 +1 @@
+bar

I want new files that are in b but not a to be created, but files that are in a but not b ignored. Is that possible with diff?

Thanks!

Best Answer

I think you're looking for GNU diff's --unidirectional-new-file flag, instead of -N.

diff -ur --unidirectional-new-file a/bar b/bar
--- a/bar   1969-12-31 19:00:00.000000000 -0500
+++ b/bar   2012-05-22 12:39:33.000000000 -0400
@@ -0,0 +1 @@
+bar
Only in a: foo

If you really want to, you can pipe the output through grep -v '^Only in' to get rid of the last line, but it shouldn't hurt anything to leave it in.

Related Question