바이오 대표

[Git] Intro - Linux에서 파일 비교하기 (diff/patch) 본문

Programming enviorment

[Git] Intro - Linux에서 파일 비교하기 (diff/patch)

바이오 대표 2021. 11. 17. 22:07

 

< diff/ patch > 

해당 코드를 통해 old_version 과 new_version을 비교 및 확인 할 수 있다. (All changes, context 확인 가능)

 

사용 이유: 사람이 직접 비교하면 시간도 많이 많이 걸릴뿐더러 error-prone

=> automatically by using diff/patch 

 

 

< diff >

$ diff [old_file] [new_file]   # difference 찾기

해당 코드의 아웃풋으로, difference 가 있는 code line 만보여준다. 두개의 파일 비교 후 < 는 old_version, > 는 new_version에서의 라인들을 보여준다.

     < line  #from [old_file]
     > line  #from [new_file]

 

만약 output에 "5c5,6" 와 같이 표시가 된다면 이는 old_version의 5번째라인이 new_version 파일 5,6라인으로 바뀜을 보여준다. 

https://man7.org/linux/man-pages/man1/diff.1.html 

 

 

< diff - u >

$ diff -u [old_file] [new_file]

shows a unified format to show the difference ("-" removed, "+" added)

        예를들어 output이 다음과 같을 때 old_version과 비교했을때  라인1은 추가된 라인이고 라인2는 삭제된 부분이다.
         + line1
         - line2 
          

$ diff -u [old_file] [new_file] > change.diff 

* > (redirect changes into generate context) 

 

 

*Difference 를 볼 수 있는 다른 commands

- wdiff (highlight words (not showing lines like diff)) , meld, KDiff3, vimdiff 

 

 

< patch >

patch 코드를 이용해서 apply changes to the original file 

$ patch [file] < change.diff

https://man7.org/linux/man-pages/man1/patch.1.html