Mina Ashraf
Mina Ashraf

Reputation: 353

How to create a patch file for text files only in a directory

I have a directory with hundreds of text files and object files. I had a copy of this directory somewhere else and I edited it and recompiled it. the object files are now different, but I want to generate a patch from the text files only. is there a way to do this or do I need to separate them into different folders?

diff -uraN original/ new/ > patch.diff

how can I specify file types in this command?

-X excludes, but I want the opposite of this. I want to exclude everything except .txt files

Upvotes: 0

Views: 960

Answers (3)

root/packages/apps/PermissionController$ git format-patch -1 commit --stdout > patch-file.txt

root/packages/modules/Permission$ git am -p2 --directory=PermissionController patch-file.txt

Upvotes: 0

Joe Herbert
Joe Herbert

Reputation: 462

If I understand your question correctly, you just want to perform the diff command on any files with the extension .txt.

Unfortunately, diff has no include option, but we can get around it by using find to get a list of the files which aren't text files, and then we can exclude those using -X. This one liner will do that.

find original new ! -name '*.txt' -printf '%f\n' -type f | diff -uraN original/ new/ -X - > patch.diff

If you want more info on how that works you can check out the man pages for find and diff.

Upvotes: 0

Paul Hodges
Paul Hodges

Reputation: 15293

Did you want one diff per txt?

for f in original/*.txt                        # for each original
do  d=${f#original/}                           # get base name
    diff -uraN "$f" "new/$d" > ${d%.txt}.diff  # diff old against new
done

You mention -X; I'm not sure how diff implements it, but the bash CLI allows extended globbing.

$: shopt -s extglob
$: ls -l *.???
-rw-r--r-- 1 P2759474 1049089         0 May 10 21:49 OCG3C82.tmp
-rw-r--r-- 1 P2759474 1049089         0 May 11 03:22 OCG511D.tmp
-rw-r--r-- 1 P2759474 1049089         0 May 12 00:03 OCG5214.tmp
-rw-r--r-- 1 P2759474 1049089         0 May 14 09:34 a.txt
-rw-r--r-- 1 P2759474 1049089         0 May 14 09:34 b.txt

$: ls *.!(txt)
-rw-r--r-- 1 P2759474 1049089         0 May 10 21:49 OCG3C82.tmp
-rw-r--r-- 1 P2759474 1049089         0 May 11 03:22 OCG511D.tmp
-rw-r--r-- 1 P2759474 1049089         0 May 12 00:03 OCG5214.tmp 

Upvotes: 2

Related Questions