{"id":18351,"date":"2023-11-23T05:57:56","date_gmt":"2023-11-23T05:57:56","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=18351"},"modified":"2023-11-23T05:57:56","modified_gmt":"2023-11-23T05:57:56","slug":"diff-command-linux-examples","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/diff-command-linux-examples\/","title":{"rendered":"diff Command Linux Examples"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1700719025098-diff%20Command%20Linux%20Examples.jpg\" alt=\"\" \/><br \/>\nThe diff command in Linux stands as a powerful tool for comparing files and directories, offering insights into the differences between them. Whether you&#8217;re a seasoned developer, a system administrator, or an enthusiastic Linux user, understanding how to use diff effectively can streamline your workflow and help manage changes within files or directories.<\/p>\n<p>This article aims to provide a comprehensive guide to the diff command, exploring its functionalities through various examples. From basic file comparisons to more advanced options, readers will gain a deeper understanding of how to leverage this versatile tool efficiently.<\/p>\n<h2>What is diff Command Linux?<\/h2>\n<p>The diff command in Linux is a powerful utility used for comparing files line by line. Here are several examples demonstrating how the diff command can be used:<\/p>\n<p><strong>Example 1: Basic File Comparison<\/strong><\/p>\n<pre><code>diff file1.txt file2.txt<\/code><\/pre>\n<p>This command compares the contents of file1.txt and file2.txt, displaying the lines that differ between the two files.<\/p>\n<p><strong>Example 2: Unified Diff Output<\/strong><\/p>\n<pre><code>diff -u file1.txt file2.txt<\/code><\/pre>\n<p>Using the -u option, this command generates a unified output that includes context lines and highlights the differences between file1.txt and file2.txt.<\/p>\n<p><strong>Example 3: Ignore White Space Changes<\/strong><\/p>\n<pre><code>diff -b file1.txt file2.txt<\/code><\/pre>\n<p>The -b option ignores changes in the amount of white space while comparing file1.txt and file2.txt.<\/p>\n<p><strong>Example 4: Compare Directories Recursively<\/strong><\/p>\n<pre><code>diff -r directory1 directory2<\/code><\/pre>\n<p>By using the -r option, this command recursively compares all files in directory1 and directory2, showing the differences between the contents.<\/p>\n<p><strong>Example 5: Creating a Patch File<\/strong><\/p>\n<pre><code>diff -u original_file modified_file &gt; patch_file.patch<\/code><\/pre>\n<p>Using the -u option, this command generates a unified diff between original_file and modified_file and saves the differences into a patch file named patch_file.patch.<\/p>\n<p><strong>Example 6: Apply a Patch File<\/strong><\/p>\n<pre><code>patch original_file  patch_file. This generates a unified diff that includes the changes between the original and modified files, saved in the specified patch file.<\/code><\/pre>\n<p><strong>3. Can diff be used to compare directories?<\/strong><br \/>\nYes, diff can compare directories. By using the -r or &#8211;recursive option along with diff, you can compare entire directories and identify differences between files within them.<\/p>\n<p><strong>4. Is there a way to ignore white spaces or empty lines during comparison?<\/strong><br \/>\nYes, diff offers options like -b (ignore changes in the amount of white space), -B (ignore changes that just insert or delete blank lines), and -w (ignore all white space) to facilitate comparison by ignoring specific types of differences.<\/p>\n<p><strong>5. How can I apply a diff patch to update a file?<\/strong><br \/>\nTo apply a diff patch and update a file, you can use the patch command. For instance, patch original_file patch_file applies the changes specified in the patch file to the original file.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The diff command in Linux stands as a powerful tool for comparing files and directories, offering insights into the differences between them. Whether you&#8217;re a seasoned developer, a system administrator, or an enthusiastic Linux user, understanding how to use diff effectively can streamline your workflow and help manage changes within files or directories. This article [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[230],"tags":[],"class_list":["post-18351","post","type-post","status-publish","format-standard","hentry","category-linux"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>diff Command Linux Examples<\/title>\n<meta name=\"description\" content=\"The diff command in Linux is a powerful utility used for comparing files line by line.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prepbytes.com\/blog\/diff-command-linux-examples\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"diff Command Linux Examples\" \/>\n<meta property=\"og:description\" content=\"The diff command in Linux is a powerful utility used for comparing files line by line.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/diff-command-linux-examples\/\" \/>\n<meta property=\"og:site_name\" content=\"PrepBytes Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prepbytes0211\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-23T05:57:56+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1700719025098-diff%20Command%20Linux%20Examples.jpg\" \/>\n<meta name=\"author\" content=\"Prepbytes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prepbytes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/diff-command-linux-examples\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/diff-command-linux-examples\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"diff Command Linux Examples\",\"datePublished\":\"2023-11-23T05:57:56+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/diff-command-linux-examples\/\"},\"wordCount\":400,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/diff-command-linux-examples\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1700719025098-diff%20Command%20Linux%20Examples.jpg\",\"articleSection\":[\"Linux\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/diff-command-linux-examples\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/diff-command-linux-examples\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/diff-command-linux-examples\/\",\"name\":\"diff Command Linux Examples\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/diff-command-linux-examples\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/diff-command-linux-examples\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1700719025098-diff%20Command%20Linux%20Examples.jpg\",\"datePublished\":\"2023-11-23T05:57:56+00:00\",\"description\":\"The diff command in Linux is a powerful utility used for comparing files line by line.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/diff-command-linux-examples\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/diff-command-linux-examples\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/diff-command-linux-examples\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1700719025098-diff%20Command%20Linux%20Examples.jpg\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1700719025098-diff%20Command%20Linux%20Examples.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/diff-command-linux-examples\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linux\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/linux\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"diff Command Linux Examples\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/43.205.93.38\/#website\",\"url\":\"http:\/\/43.205.93.38\/\",\"name\":\"PrepBytes Blog\",\"description\":\"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING\",\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/43.205.93.38\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/43.205.93.38\/#organization\",\"name\":\"Prepbytes\",\"url\":\"http:\/\/43.205.93.38\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"contentUrl\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"width\":160,\"height\":160,\"caption\":\"Prepbytes\"},\"image\":{\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/prepbytes0211\/\",\"https:\/\/www.instagram.com\/prepbytes\/\",\"https:\/\/www.linkedin.com\/company\/prepbytes\/\",\"https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\",\"name\":\"Prepbytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"caption\":\"Prepbytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"diff Command Linux Examples","description":"The diff command in Linux is a powerful utility used for comparing files line by line.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/prepbytes.com\/blog\/diff-command-linux-examples\/","og_locale":"en_US","og_type":"article","og_title":"diff Command Linux Examples","og_description":"The diff command in Linux is a powerful utility used for comparing files line by line.","og_url":"https:\/\/prepbytes.com\/blog\/diff-command-linux-examples\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2023-11-23T05:57:56+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1700719025098-diff%20Command%20Linux%20Examples.jpg","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/diff-command-linux-examples\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/diff-command-linux-examples\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"diff Command Linux Examples","datePublished":"2023-11-23T05:57:56+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/diff-command-linux-examples\/"},"wordCount":400,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/diff-command-linux-examples\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1700719025098-diff%20Command%20Linux%20Examples.jpg","articleSection":["Linux"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/diff-command-linux-examples\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/diff-command-linux-examples\/","url":"https:\/\/prepbytes.com\/blog\/diff-command-linux-examples\/","name":"diff Command Linux Examples","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/diff-command-linux-examples\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/diff-command-linux-examples\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1700719025098-diff%20Command%20Linux%20Examples.jpg","datePublished":"2023-11-23T05:57:56+00:00","description":"The diff command in Linux is a powerful utility used for comparing files line by line.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/diff-command-linux-examples\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/diff-command-linux-examples\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/diff-command-linux-examples\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1700719025098-diff%20Command%20Linux%20Examples.jpg","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1700719025098-diff%20Command%20Linux%20Examples.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/diff-command-linux-examples\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"Linux","item":"https:\/\/prepbytes.com\/blog\/category\/linux\/"},{"@type":"ListItem","position":3,"name":"diff Command Linux Examples"}]},{"@type":"WebSite","@id":"http:\/\/43.205.93.38\/#website","url":"http:\/\/43.205.93.38\/","name":"PrepBytes Blog","description":"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING","publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/43.205.93.38\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/43.205.93.38\/#organization","name":"Prepbytes","url":"http:\/\/43.205.93.38\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/","url":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","contentUrl":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","width":160,"height":160,"caption":"Prepbytes"},"image":{"@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/prepbytes0211\/","https:\/\/www.instagram.com\/prepbytes\/","https:\/\/www.linkedin.com\/company\/prepbytes\/","https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA"]},{"@type":"Person","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e","name":"Prepbytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","caption":"Prepbytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/18351","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=18351"}],"version-history":[{"count":1,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/18351\/revisions"}],"predecessor-version":[{"id":18352,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/18351\/revisions\/18352"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=18351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=18351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=18351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}