{"id":11244,"date":"2021-10-28T13:26:31","date_gmt":"2021-10-28T13:26:31","guid":{"rendered":"https:\/\/linuxways.net\/?p=11244"},"modified":"2021-10-28T13:26:31","modified_gmt":"2021-10-28T13:26:31","slug":"diff-command-in-linux","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/centos\/diff-command-in-linux\/","title":{"rendered":"diff Command in Linux"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p>diff is an acronym for the difference. It is a useful command used to compare the differences between files based on the lines of the file.<\/p>\n<p>This command just tells us which line to change in the file to make the 2 files the same. To be more precise, it creates a list of changes in the first file to match the second file.<\/p>\n<p>This tutorial will guide you on how to compare two text files by the diff command in Linux as you go through below.<\/p>\n<h2>The syntax of diff command<\/h2>\n<pre>$ diff [options] file1 file2<\/pre>\n<p>You need to remember some special symbols of diff:<\/p>\n<p>a: add<\/p>\n<p>c: change<\/p>\n<p>d: delete<\/p>\n<p>For example, I have 2 files a.txt and b.txt<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1042\" height=\"270\" class=\"wp-image-11245\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-329.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-329.png 1042w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-329-300x78.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-329-1024x265.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-329-768x199.png 768w\" sizes=\"auto, (max-width: 1042px) 100vw, 1042px\" \/><\/p>\n<p>Now we will try to use the diff command with these 2 files:<\/p>\n<pre>$ diff a.txt b.txt<\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1043\" height=\"234\" class=\"wp-image-11246\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-330.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-330.png 1043w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-330-300x67.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-330-1024x230.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-330-768x172.png 768w\" sizes=\"auto, (max-width: 1043px) 100vw, 1043px\" \/><\/p>\n<p>This command will print out:<\/p>\n<ul>\n<li>Corresponding line number.<\/li>\n<li>Special symbols.<\/li>\n<\/ul>\n<p><strong>Definition<\/strong>:<\/p>\n<p>0a1,2 means from line 0 you need to <strong>a<\/strong>dd Malaysia and Campuchia to lines 1, 2 of the first file.<\/p>\n<p>3,5c5 means from line 3 to line 5 of the first file you need to <strong>c<\/strong>hange it to match line 5 of the second file.<\/p>\n<p>&#8212; to separate line 1 from line 2.<\/p>\n<h2>Options<\/h2>\n<p><strong>1. -c (context)<\/strong>: Provides context to easily spot the differences<\/p>\n<p>We will apply with files a.txt and b.txt:<\/p>\n<pre>$ diff -c a.txt b.txt<\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1042\" height=\"346\" class=\"wp-image-11247\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-331.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-331.png 1042w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-331-300x100.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-331-1024x340.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-331-768x255.png 768w\" sizes=\"auto, (max-width: 1042px) 100vw, 1042px\" \/><\/p>\n<p>*** to specify the first file<\/p>\n<p>&#8212; to specify the second file<\/p>\n<p>+ to specify lines that need to add to the first file<\/p>\n<p>! to specify lines that changed<\/p>\n<p><strong>2. -u (unified):<\/strong> To check differences in unified mode<\/p>\n<pre>$ diff -u a.txt b.txt<\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1043\" height=\"272\" class=\"wp-image-11248\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-332.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-332.png 1043w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-332-300x78.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-332-1024x267.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-332-768x200.png 768w\" sizes=\"auto, (max-width: 1043px) 100vw, 1043px\" \/><\/p>\n<p>&#8212; to specify the first file<\/p>\n<p>+++ to specify the second file<\/p>\n<p>@@ mark the beginning of changes and which line of each file<\/p>\n<p><strong>3. -i<\/strong>: To case sensitive<\/p>\n<p>For example, I have 2 files a.txt and b.txt<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1042\" height=\"151\" class=\"wp-image-11249\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-333.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-333.png 1042w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-333-300x43.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-333-1024x148.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-333-768x111.png 768w\" sizes=\"auto, (max-width: 1042px) 100vw, 1042px\" \/><\/p>\n<p>Now we will try to use the diff command with<strong> -i<\/strong> option with these 2 files:<\/p>\n<pre>$ diff -i a.txt b.txt<\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1040\" height=\"128\" class=\"wp-image-11250\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-334.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-334.png 1040w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-334-300x37.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-334-1024x126.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-334-768x95.png 768w\" sizes=\"auto, (max-width: 1040px) 100vw, 1040px\" \/><\/p>\n<p><strong>4. &#8211;version<\/strong>: Check your version<\/p>\n<pre>$ diff --version<\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1041\" height=\"207\" class=\"wp-image-11251\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-335.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-335.png 1041w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-335-300x60.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-335-1024x204.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/10\/word-image-335-768x153.png 768w\" sizes=\"auto, (max-width: 1041px) 100vw, 1041px\" \/><\/p>\n<h2>Conclusion<\/h2>\n<p>In this article, we guided you on how to compare two text files by the diff command in Linux.<\/p>\n<p>Thanks for reading!<\/p>","protected":false},"excerpt":{"rendered":"<p>Introduction diff is an acronym for the difference. It is a useful command used to compare the differences between files based on the lines of the file. This&hellip;<\/p>","protected":false},"author":1,"featured_media":11668,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,4,5,83,165,2],"tags":[633,35],"class_list":["post-11244","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-centos","category-debian","category-mint","category-opensuse","category-red-hat","category-ubuntu","tag-diff-command","tag-linux"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/11244","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=11244"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/11244\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/11668"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=11244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=11244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=11244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}