본문 바로가기

IT

리눅스 파일비교 방법

여러대의 서버를 구축하던 중 특정 서버에 환경설정 문제로 오동작이 발생하는 경우가 흔히 발생합니다.

이런 상황에서 정상서버의 설정파일과 오동작 서버의 두 설정값을 DIFF 명령어를 통해서 쉽게 파일을 비교할 수 있습니다. 


자주 사용하는 "--brief , -y, -r " 옵션 



A : httpd-1.conf

B : httpd-2.conf 

A, B 두개의 파일을 diff 명령어를 통해서 --brief 옵션으로 실행할 경우 아래의 결과와 같이

동일하지 않은 내용이 있으면 " ~~~~differ" 결과값을 출력합니다. 

--brief 옵션 

[root@nh1 http]# diff --brief httpd-1.conf httpd-2.conf 

Files httpd-1.conf and httpd-2.conf differ


A: httpd-1.conf 

B : httpd.conf 

A, B 두개의 파일이 동일한 내용이라면 아무 내용이 출력이 되지 않습니다. 

[root@nh1 http]# diff --brief httpd-1.conf httpd.conf 

[root@nh1 http]# 




그렇다면 두 파일중에서 어떤 부분의 내용이 다른 것인지 까지 찾을 수 있을까요? 
네 ~ 가능합니다. 

제일 첫줄에는 두 파일의 내용이 다른 부분의 라인수를 의미하는 숫자가 나오게 되고, 그 다음으로 두개의 파일에 각각 다른 내용이 표시됩니다. -r 옵션 

[root@nh1 http]# diff -r httpd-1.conf httpd-2.conf 
134c134
< #Listen 80
---
> Listen 80



그럼 마지막으로 두개의 파일을 왼쪽|오른쪽  으로분할에서 화면단위로 볼 수 있는 방법은 아래와 같습니다.  -y 옵션 

[root@nh1 http]# diff -y httpd-1.conf httpd-2.conf | more
#                                                               #
# This is the main Apache server configuration file.  It cont   # This is the main Apache server configuration file.  It cont
# configuration directives that give the server its instructi   # configuration directives that give the server its instructi
# See <URL:http://httpd.apache.org/docs/2.2/> for detailed in   # See <URL:http://httpd.apache.org/docs/2.2/> for detailed in
# In particular, see                                            # In particular, see
# <URL:http://httpd.apache.org/docs/2.2/mod/directives.html>    # <URL:http://httpd.apache.org/docs/2.2/mod/directives.html>
# for a discussion of each configuration directive.             # for a discussion of each configuration directive.
#                                                               #
#                                                               #
# Do NOT simply read the instructions in here without underst   # Do NOT simply read the instructions in here without underst
# what they do.  They're here only as hints or reminders.  If   # what they do.  They're here only as hints or reminders.  If
# consult the online docs. You have been warned.                # consult the online docs. You have been warned.  
#                                                               #
# The configuration directives are grouped into three basic s   # The configuration directives are grouped into three basic s
#  1. Directives that control the operation of the Apache ser   #  1. Directives that control the operation of the Apache ser
#     whole (the 'global environment').                         #     whole (the 'global environment').





반응형