diff 명령어 (AIX 5.3·6.1)
https://archive.horanglab.com/posts/aix-cmd-diff
핵심 정리
- 텍스트 파일 두 개, 또는 두 디렉터리의 같은 이름 파일을 비교해, 같게 만들려면 어떤 줄을 바꿔야 하는지 보여 줍니다.
- 기본 출력은
ed명령과 비슷한2a3,4d3,5,7c5,6형태이고, 첫 파일의 줄은<, 둘째 파일의 줄은>로 시작합니다. - 형식:
-c·-C N(문맥 형식),-u·-U N(unified 형식, 6.1),-e(ed 스크립트),-f(역순),-n(RCS 형식),-D 문자열(전처리기 병합). - 비교 방식:
-b(공백 양 무시),-w(공백·탭 모두 무시),-i(대소문자 무시). 디렉터리는-r(재귀),-s(같은 파일도 보고),-S 파일. - 종료 상태는 0(같음), 1(다름), 2 이상(오류)입니다. 파일 이름 자리에
-를 쓰면 표준 입력을 읽습니다.
| 항목 | 내용 |
|---|---|
| 버전 | AIX 5.3, AIX 6.1 (6.1에서 -u, -U 플래그가 더해짐) |
| 원문 | IBM Commands Reference — 5.3 Volume 2, 6.1 Volume 2 |
| 파일 | /usr/bin/diff |
| 관련 명령어 | cmp, bdiff, ed, pr |
목적
텍스트 파일을 비교합니다.
영어 원문
Compares text files.
구문
두 파일의 내용을 비교할 때:
diff [ -c | -C Lines | -D [ String ] | -e | -f | -n | -u | -U Lines ] [ -b ] [ -i ] [ -t ] [ -w ] File1 File2
diff [ -h ] [ -b ] File1 File2
디렉터리의 내용을 정렬해 서로 다른 파일을 비교할 때:
diff [ -c | -C Lines | -e | -f | -n | -u | -U Lines ] [ -b ] [ -i ] [ -l ] [ -r ] [ -s ] [ -S File ] [ -t ] [ -w ]
Directory1 Directory2
diff [ -h ] [ -b ] Directory1 Directory2
-u와 -U는 6.1 구문에만 있습니다.
설명
diff 명령은 텍스트 파일을 비교합니다. 파일 하나씩 비교하거나 디렉터리의 내용을 비교할 수 있습니다.
참고: diff는 입력 파일이 텍스트 파일일 때만 동작합니다.
Directory1과 Directory2를 지정하면 두 디렉터리에서 이름이 같은 텍스트 파일을 비교합니다. 서로 다른 바이너리 파일, 공통 하위 디렉터리, 한쪽 디렉터리에만 있는 파일을 나열합니다.
일반 파일을 비교할 때와 디렉터리 비교 중 서로 다른 텍스트 파일을 비교할 때, 두 파일을 같게 만들려면 어떤 줄을 바꿔야 하는지 알려 줍니다. File1과 File2가 둘 다 디렉터리가 아니면 어느 한쪽을 -(빼기)로 주어 표준 입력을 쓸 수 있습니다. File1이 디렉터리면 그 안에서 File2와 이름이 같은 파일을 씁니다.
보통의 출력은 다음 형태의 줄로 이뤄집니다.
| File1에서 바뀌는 줄 | 동작 | File2에서 바뀌는 줄 |
|---|---|---|
Number1 |
a(더함) |
Number2[,Number3] |
Number1[,Number2] |
d(지움) |
Number3 |
Number1[,Number2] |
c(바꿈) |
Number3[,Number4] |
이 줄들은 File1을 File2로 바꾸는 ed 하위 명령과 비슷합니다. 동작 글자 앞의 숫자는 File1, 뒤의 숫자는 File2의 줄 번호입니다. 그래서 a와 d를 맞바꿔 오른쪽에서 왼쪽으로 읽으면 File2를 File1로 바꾸는 법도 알 수 있습니다. ed처럼 같은 수의 짝(Number1 = Number2)은 숫자 하나로 줄여 씁니다.
이 줄마다 뒤에, 첫 파일에서 바뀌는 줄을 모두 <(작다 기호)를 붙여 보여 주고, 둘째 파일에서 바뀌는 줄을 모두 >(크다 기호)를 붙여 보여 줍니다.
종료 값 0은 차이 없음, 1은 차이 있음, 2는 오류입니다.
참고: -c, -C, -D, -e, -f, -n, -u, -U 중 둘 이상을 지정하면 명령줄의 마지막 것이 우선하며, 오류 메시지는 내지 않습니다.
영어 원문
The diff command compares text files. It can compare single files or the contents of directories.
Note: The diff command only works with input files that are text files.
If the Directory1 and Directory2 parameters are specified, the diff command compares the text files that have the same name in both directories. Binary files that differ, common subdirectories, and files that appear in only one directory are listed.
When the diff command is run on regular files, and when comparing text files that differ during directory comparison, the diff command tells what lines must be changed in the files to make them agree. If neither the File1 nor File2 parameter is a directory, then either may be given as - (minus sign), in
which case the standard input is used. If the File1 parameter is a directory, then a file in that directory whose file name is the same as the File2 parameter is used.
The typical output contains lines of these forms:
| Lines Affected in File1 | Action | Lines Affected in File2 |
|---|---|---|
Number1 |
a |
Number2[,Number3] |
Number1[,Number2] |
d |
Number3 |
Number1[,Number2] |
c |
Number3[,Number4] |
These lines resemble ed subcommands to convert File1 into File2. The numbers before the action letters pertain to File1; those after pertain to File2. Thus, by exchanging a for d and reading from right to left, you can also tell how to convert File2 into File1. As in the ed command, identical pairs (where Number1 = Number2) are abbreviated as a single number.
Following each of these lines, the diff command displays all lines affected in the first file preceded by a <: (less than sign, colon), then displays all lines affected in the second file are preceded by a > (greater than sign).
An exit value of 0 indicates no differences, 1 indicates differences found, and 2 indicates an error.
Note: If more than one of the -c, -C, -D, -e, -f, or -n, -u, or -U flags are specified, the last one on the command line takes precedence. The system does not issue an error message.
플래그
| 플래그 | 설명 |
|---|---|
-b |
줄 끝의 공백은 양과 상관없이 줄바꿈 하나로 보고(줄바꿈 앞의 공백 문자는 무시), 그 밖의 공백 문자 묶음(줄바꿈 제외)은 같은 것으로 비교합니다. |
-C Lines |
복사한 문맥 줄을 Lines만큼 넣어 비교 결과를 냅니다(문맥 형식, 아래 설명). |
-c |
복사한 문맥 줄을 세 줄 넣어 비교 결과를 냅니다(문맥 형식, 아래 설명). |
-D [ String ] |
File1과 File2를 합친 판을 표준 출력에 만듭니다. C 전처리기 제어문을 넣어, String을 정의하지 않고 컴파일하면 File1을, 정의하고 컴파일하면 File2를 컴파일한 것과 같게 합니다. |
-e |
File1을 File2로 바꾸는 ed 편집기용 형식으로 출력합니다(아래 설명). |
-f |
File1을 File2로 바꾸는 데 필요한 수정을 -e와 반대 순서로 보여 줍니다. ed에는 쓸 수 없는 형식입니다. |
-h |
바뀐 부분이 짧고 서로 멀리 떨어져 있을 때 더 빠를 수 있는 다른 방식으로 비교합니다. 파일 길이와 상관없이 동작합니다. -c, -C, -D, -e, -f, -n과 함께 쓸 수 없고, -b를 뺀 다른 플래그는 무시합니다. |
-i |
대소문자를 무시합니다. 예를 들어 소문자 a와 대문자 A를 같게 봅니다. |
-l |
긴 출력 형식입니다. 텍스트 파일 비교 결과마다 pr 명령으로 쪽을 나눕니다. 다른 차이는 기억해 두었다가 텍스트 파일의 차이를 모두 보고한 뒤에 요약합니다. |
-n |
-e와 비슷하지만 순서가 반대이고, 넣기·지우기 명령마다 바뀐 줄 수를 붙입니다. RCS(revision control system)가 쓰는 형식입니다. |
-r |
만나는 공통 하위 디렉터리에도 diff를 재귀적으로 적용합니다. |
-s |
같은 파일, 그 밖에 언급되지 않는 파일도 보고합니다. |
-S [ File ] |
디렉터리를 비교할 때, 대조 순서에서 File보다 앞서는 이름의 파일은 무시합니다. Directory1과 Directory2에만 적용되며, -r과 함께 써도 하위 디렉터리에는 재귀적으로 적용되지 않습니다. |
-t |
출력 줄의 탭을 펼칩니다. 보통 출력이나 -c 출력은 줄 앞에 문자를 붙여 원본의 들여쓰기가 어긋나 읽기 어려운데, 이 플래그는 원본의 들여쓰기를 유지합니다. |
-u |
통합(unified) 문맥 세 줄로 비교 결과를 냅니다. -c와 비슷하지만 문맥 줄을 되풀이하지 않고 문맥, 지운 줄, 더한 줄을 섞어 함께 보여 줍니다. 6.1부터 |
-U Lines |
통합 문맥 Lines줄로 비교 결과를 냅니다. -C와 비슷하지만 문맥, 지운 줄, 더한 줄을 섞어 함께 보여 줍니다. 6.1부터 |
-w |
빈칸과 탭을 모두 무시하고, 그 밖의 공백 묶음은 같게 봅니다. 예를 들어 if ( a == b )와 if(a==b)를 같게 봅니다. |
문맥 형식(-c, -C)
출력은 관련 파일 이름과 만든 날짜로 시작합니다. 바뀐 곳마다 *(별표) 열두 개로 된 줄로 나눕니다. File1에서 지운 줄에는 -(빼기), File2에 더한 줄에는 +(더하기), 양쪽에서 바뀐 줄에는 !(느낌표)를 붙입니다. 지정한 문맥 줄 수 안에 있는 변경은 한 묶음으로 출력합니다.
-e로 여러 판 관리하기
다음 셸 프로그램으로 파일의 여러 판을 관리할 수 있습니다. 원본 파일($1)과, diff가 만든 판에서 판으로의 ed 스크립트($2, $3, …)만 있으면 최신 판을 표준 출력으로 얻습니다.
(shift; cat $*; echo '1,$p') | ed - $1
디렉터리를 -e로 비교하면 출력에 명령이 더 붙어, 두 디렉터리에 공통인 텍스트 파일을 Directory1의 상태에서 Directory2의 상태로 바꾸는 셸 스크립트가 됩니다.
참고: -e나 -f가 만든 편집 스크립트로는 .(마침표) 하나만 있는 줄을 만들 수 없습니다.
영어 원문
-b— Causes any amount of white space at the end of a line to be treated as a single newline character (the white-space characters preceding the newline character are ignored) and other strings of white-space characters, not including newline characters, to compare equally.-CLines — Produces a diff command comparison with a number of lines of copied context equal to the value specified by the Lines variable. The-Cflag modifies the output slightly. The output begins with identification of the files involved and their creation dates. Each change is separated by a line with a dozen*(asterisks). The lines removed from File1 are marked with a-(minus sign ) and those added to File2 are marked with a+(plus sign). Lines changed from one file to the other are marked in both files with an!(exclamation point). Changes that lie within the specified copied context lines of each other are grouped together as output.-c— Produces a diff command comparison with three lines of copied context. The-cflag modifies the output slightly. The output begins with identification of the files involved and their creation dates. Each change is separated by a line with a dozen*(asterisks). The lines removed from File1 are marked with a-(minus sign ) and those added to File2 are marked with a+(plus sign). Lines changed from one file to the other are marked in both files with an!(exclamation point). Changes within the specified copied context lines of each other are grouped together as output.-D[ String ] — Causes the diff command to create a merged version of File1 and File2 on the standard output. The C preprocessor controls are included so that a compilation of the result without defining String is equivalent to compiling File1, while defining String yields File2.-e— Produces output in a form suitable for use with the ed editor to convert File1 to File2. When using this flag, the following shell program may help maintain multiple versions of a file. Only an ancestral file ($1) and a chain of version-to-version ed scripts ($2,$3, ...) made by the diff command need to be on hand. The latest version appears on the standard output as follows:(shift; cat $*; echo ’1,$p’) | ed - $1Extra commands are added to the output when the-eflag is used to compare directories, so the result is a shell script for converting text files that are common to the two directories from their state in Directory1 to their state in Directory2. Note: Editing scripts produced by the-eor-fflags cannot create lines consisting of a single . (period).-f— Produces output in a form not suitable for use with the ed editor, showing the modifications necessary to convert File1 to File2 in the reverse order of that produced under the-eflag.-h— Performs an alternate comparison that may be faster if the changed sections are short and well separated. The-hflag works on files of any length. The-c,-C,-D,-e,-f, and-nflags cannot be used with the-hflag. All other flags except the-bflag are ignored when used with the-hflag.-i— Ignores the case of letters. For example, a lowercase a is treated the same as an uppercase A.-l— Long output format. Each result from the diff command text file comparison is piped through the pr command for pagination. Other differences are remembered and summarized after all text file differences are reported.-n— Produces output similar to that of the-eflag, but in the opposite order and with a count of changed lines on each insert or delete command. This is the form used by the revision control system (RCS).-r— Causes application of the diff command recursively to common subdirectories encountered.-s— Reports files that are the same and otherwise not mentioned.-S[ File ] — Ignores files whose names collate before the file specified by the File variable when comparing directories. The-Sflag only applies to the directories specified in the Directory1 and Directory2 parameters. If you use the-rflag with the-Sflag, the-Sflag does not work recursively in the Directory1 and Directory2 subdirectories.-t— Expands tabs in output lines. Typical output or the-cflag output adds characters to the front of each line, which may affect indentation of the original source lines and makes the output listing difficult to interpret. This flag preserves the original source's indentation.-u— Produces a diff command comparison with three lines of unified context. The output is similar to that of the-cflag, except that the context lines are not repeated; instead, the context, deleted, and added lines are shown together, interleaved.-ULines — Produces a diff command comparison with a number of lines of unified context equal to the value specified by the Lines variable. The output is similar to that of the-Cflag, except that the context lines are not repeated; instead, the context, deleted, and added lines are shown together, interleaved.-w— Ignores all spaces and tab characters and treats all other strings of blanks as equivalent. For example,if ( a == b )compares equally toif(a==b).
종료 상태
| 값 | 뜻 |
|---|---|
| 0 | 차이가 없습니다. |
| 1 | 차이가 있습니다. |
| 1보다 큼 | 오류가 났습니다. |
영어 원문
This command returns the following exit values:
- 0 — No differences were found.
- 1 — Differences were found.
>1— An error occurred.
예제
- 두 파일을 비교합니다.
chap1.bak와chap1의 차이를 보여 줍니다.
diff chap1.bak chap1
- 공백의 양 차이를 무시하고 두 파일을 비교합니다. 단어 사이의 빈칸·탭 수만 다른 줄은 같다고 봅니다.
diff -w prog.c.bak prog.c
- 한 파일로부터 다른 파일을 다시 만드는
ed명령이 든 파일을 만듭니다.new.to.old.ed에는chap2를chap2.old의 내용으로 되돌리는ed하위 명령이 들어 있습니다. 대개new.to.old.ed는chap2.old보다 훨씬 작으므로,chap2.old를 지워 디스크 공간을 아끼고 필요할 때 다시 만들 수 있습니다.
diff -e chap2 chap2.old >new.to.old.ed
다시 만들 때는 다음과 같이 입력합니다.
(cat new.to.old.ed ; echo '1,$p') | ed - chap2 >chap2.old
괄호 안의 명령은 ed로 보낼 편집 명령 끝에 1,$p를 붙입니다. 1,$p는 편집한 뒤 파일을 표준 출력에 쓰게 합니다. 이렇게 고친 명령을 파이프(| ed)로 ed에 넘기면 편집기가 표준 입력으로 읽습니다. - 플래그는 ed가 파일 크기 같은 부가 정보를 표시하지 않게 해서, chap2.old의 내용에 섞이지 않게 합니다.
영어 원문
- To compare two files, enter:
diff chap1.back chap1
This displays the differences between the files chap1.bak and chap1.
- To compare two files while ignoring differences in the amount of white space, enter:
diff -w prog.c.bak prog.c
If two lines differ only in the number of spaces and tabs between words, the diff -w command considers them to be the same.
- To create a file containing commands that the ed command can use to reconstruct one file from another, enter:
diff -e chap2 chap2.old >new.to.old.ed
This creates a file named new.to.old.ed that contains the ed subcommands to change chap2 back into the version of the text found in chap2.old. In most cases, new.to.old.ed is a much smaller file than chap2.old. You can save disk space by deleting chap2.old, and you can reconstruct it at any time by entering:
(cat new.to.old.ed ; echo '1,$p') | ed - chap2 >chap2.old
The commands in parentheses add 1,$p to the end of the editing commands sent to the ed editor. The 1,$p causes the ed command to write the file to standard output after editing it. This modified command sequence is then piped to the ed command (| ed), and the editor reads it as standard input. The - flag causes the ed command not to display the file size and other extra information because it would be mixed with the text of chap2.old.
파일
| 파일 | 설명 |
|---|---|
/usr/bin/diff |
diff 명령이 들어 있습니다. |
관련 명령어: bdiff, cmp, pr
영어 원문
- /usr/bin/diff — Contains the diff command.
Related information:
- bdiff command
- cmp command
- pr command
- Input and output redirection
리눅스(GNU)와 다른 점
| 항목 | AIX 5.3·6.1 | GNU diff |
|---|---|---|
-u·-U |
6.1부터(5.3에는 없음) | 있음 |
-h |
짧은 변경에 빠른 다른 비교 방식 | 받지만 무시 |
-l |
pr로 쪽을 나눈 긴 출력 |
--paginate(-l) |
| 바이너리 파일 | 텍스트 파일에서만 동작 | --text로 바이너리도 줄 단위 비교 |
AIX 5.3·6.1 문서에는 -y(나란히 보기), -N(없는 파일을 빈 파일로), -x·--exclude, -q 같은 GNU 플래그가 없습니다. AIX 5.3에서 패치용 unified diff가 필요하면 GNU diff를 따로 설치해야 했습니다.