The command diff from the package diffutils compares files line by line. It comes in very handy when you would like to check if there were recent changes to a file since the last backup and what they are. An exit status of 0 means no differences were found, 1 means some differences were found, and 2 means trouble. And of course, if there are changes, diff will show them to you. But let’s take a look at some examples.
Example 1: Both files have the same content
Alice and Bob are each having a barbecue and have published their menus. To decide which menu I would enjoy most, I could compare the menus as follows:
$ diff menu1 menu2
$ echo $?
0
$ cat menu{1,2}
# The menu
* Spare Ribs
* Brisket
* Pulled Pork
# The menu
* Spare Ribs
* Brisket
* Pulled Pork
You see, both are serving spare ribs, brisket, and pulled pork. There is no difference between both files. Maybe Alice and Bob should join forces to host a really huge barbecue.
When Eve heard that Alice and Bob were throwing parties, she got jealous and decided to host a barbecue as well. Her menu comes with the file menu3. Well, let’s see what the differences in her menu are compared to Alice's and Bob's.
Example 2: Now there are drinks involved
$ diff menu1 menu3
4a5,8
>
> # Drinks
> * Soda
> * Beer
$ echo $?
1
$
There are differences now. And, with the output in the first line, diff tells us what has to be added (a) to menu1 so that it has the same content as menu3. So, 4a5,8 means that you have to add some lines after line 4 in menu1 so that it looks like lines 5-8 in menu3.
When switching the position of the files, the output looks different:
$ diff menu3 menu1
5,8d4
<
< # Drinks
< * Soda
< * Beer
Now, diff is telling us that the lines 5-8 in menu3 have to be deleted (d) to make the file identical with menu1.
Example 3: When there is something to change
I’ve edited menu1 and menu2 again to look like this:
# The menu
* Spare Ribs
* Brisket
* Pulled Pork
# The menu
* Spare Ribs
* Pulled Pork
* Pulled Pork
This time, diff is going to show us which line has to be changed (c) to make both files look the same:
$ diff menu1 menu2
3c3
< * Brisket
---
> * Pulled Pork
Wrapping it up
The diff command is an easy and flexible to use tool to compare files line by line. Take a look at diff(1) to figure out what else can be done with diff.
[ Want to test your sysadmin skills? Take a skills assessment today. ]
저자 소개
Jörg has been a Sysadmin for over ten years now. His fields of operation include Virtualization (VMware), Linux System Administration and Automation (RHEL), Firewalling (Forcepoint), and Loadbalancing (F5). He is a member of the Red Hat Accelerators Community and author of his personal blog at https://www.my-it-brain.de.
유사한 검색 결과
Behind the scenes of RHEL 10, part 3
Alliander modernises its electricity grid with Red Hat for long-term reliability in balance with rapid innovation
The Overlooked Operating System | Compiler: Stack/Unstuck
Linux, Shadowman, And Open Source Spirit | Compiler
채널별 검색
오토메이션
기술, 팀, 인프라를 위한 IT 자동화 최신 동향
인공지능
고객이 어디서나 AI 워크로드를 실행할 수 있도록 지원하는 플랫폼 업데이트
오픈 하이브리드 클라우드
하이브리드 클라우드로 더욱 유연한 미래를 구축하는 방법을 알아보세요
보안
환경과 기술 전반에 걸쳐 리스크를 감소하는 방법에 대한 최신 정보
엣지 컴퓨팅
엣지에서의 운영을 단순화하는 플랫폼 업데이트
인프라
세계적으로 인정받은 기업용 Linux 플랫폼에 대한 최신 정보
애플리케이션
복잡한 애플리케이션에 대한 솔루션 더 보기
가상화
온프레미스와 클라우드 환경에서 워크로드를 유연하게 운영하기 위한 엔터프라이즈 가상화의 미래