본문 바로가기

linux

[linux] 파일 비교 방법

1. diff

diff 명령이 일반적

  • 기본 사용법:
    diff file1.txt file2.txt
  • 출력: 두 파일의 차이를 출력. 첫번째 파일을 두번째 파일과 같게 하기 위한 정보도 제공
  • Options:
    • -y or --side-by-side: 하나씩 출력
    • -i: 대소문자 구분 무시
    • -w: 공백 무시
    • --color: 출력을 컬러로 표시

2. cmp

The cmp command is simpler and faster than diff when you just want to check if the files are identical or where they first differ.

  • Basic Usage:
    cmp file1.txt file2.txt
  • Output: 기본으로, 두 파일이 다른 곳을 먼저 출력하고 만일 두 파일이 동일하면 아무것도 출력하지 않는다. 빠르게 두 파일이 다른지 같은지를 파악할 때 유용
  • Options:
    • -l: 바이트 번호들을 출력, 모든 다른 곳의 바이트 값 출력
    • -s: Silent mode, only return an exit status.

3. comm

The comm 두 개의 정렬된 파일을 비교할 때 사용. 두 파일에서 공통점을 찾거나 다른 점을 찾을 때 유용

  • Basic Usage:
    comm file1.txt file2.txt
  • Output: 기본으로, 세 칸으로 출력 첫번쨰 칸은 file1.txt에만 있는 라인, 두번쨰 칸은 file2.txt에만 있는 파일, 세번쨰 칸은 두 파일 모두에서 공통으로 있는 파일을 출력
  • Options:
    • -1: Suppress the output of column 1 (lines unique to the first file).
    • -2: Suppress the output of column 2 (lines unique to the second file).
    • -3: Suppress the output of column 3 (lines that appear in both files).

4. vimdiff or vim -d

vimdiff Vim 에디터를 사용해서 비교하고 파일을 하나씩 편집

  • Basic Usage:
    vimdiff file1.txt file2.txt
  • Features: It provides a side-by-side comparison and the differences are highlighted. You can navigate through differences and copy changes from one file to another. It's particularly useful for interactive and visual comparison.

Tips for Comparing Files:

  • File Sorting: For some tools like comm, make sure files are sorted if they are supposed to be. You can sort files using the sort command.
  • Regular Expressions: For more complex comparisons, consider tools like awk or grep that can filter and compare lines based on patterns.
  • Graphical Tools: If you prefer a graphical interface, tools like Meld, Diffuse, or KDiff3 provide visual comparisons and are available for most Linux distributions.

Each of these tools has its own set of options and features, so it's beneficial to refer to the tool's manual page (man command-name) for more detailed information and advanced usage. Choose the tool based on your specific needs, whether you're looking for a quick comparison, a detailed analysis, or a visual representation of differences.

'linux' 카테고리의 다른 글

환경변수  (0) 2024.02.12
[nc] NetCat: Versatile Network Utility for TCP/UDP  (0) 2024.02.12
[linux] Monitor GPU Power Consuption  (0) 2024.02.12
[linux] 디렉토리 접근 권한 정보 변경  (0) 2024.02.12
Permission Denied  (1) 2024.02.12