Let diff create new files, but not remove existing files

5,078

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.

Share:
5,078

Related videos on Youtube

Christoph
Author by

Christoph

Updated on September 18, 2022

Comments

  • Christoph
    Christoph over 1 year

    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!

    • Christoph
      Christoph almost 12 years
      I do understand why the current behavior makes sense, just wondering if there's another way as well.
  • Christoph
    Christoph almost 12 years
    Wow, how'd I miss that? I saw that option and read it, and for some reason my mind made it out to say the reverse of what it does say. Thanks!
  • Christoph
    Christoph almost 12 years
    Actually this does work the wrong way. When I do that, it tries to create bar in b, which already exists. I can't just do -R either, since others work okay. Any ideas?
  • Jander
    Jander almost 12 years
    I think I'm misunderstanding something. It sounds like you're generating a patch to turn a into b, then applying it to a directory that's already like b, rather than one that's like a.