Software & Apps Linux 38 38 people found this article helpful How To Compare Two Text Files Using Linux Test and see if your files have updated or changed by Gary Newell Writer Gary Newell was a freelance contributor, application developer, and software tester with 20+ years in IT, working on Linux, UNIX, and Windows. our editorial process Gary Newell Updated on April 16, 2020 reviewed by Jessica Kormos Lifewire Tech Review Board Member Jessica Kormos is a writer and editor with 15 years' experience writing articles, copy, and UX content for Tecca.com, Rosenfeld Media, and many others. our review board Article reviewed on Mar 28, 2020 Jessica Kormos Linux Switching from Windows Tweet Share Email This guide shows how to use Linux to compare two files and output the differences between the files to the screen or a file. You won't install special software to compare files using Linux, but you need to know how to open a terminal window. The quickest way to open a terminal window in Linux is to press the CTRL+ALT+T keys at the same time. Lifewire / Derek Abella Create the Files to Compare If you want to follow along with this guide, create two text files that contain similar but different text. Create the First File Create a file called file1 and then enter the following text: 10 green bottles standing on a wall10 green bottles standing on a wallIf one green bottle should accidentally fallThere would be 9 green bottles standing on the wall To create this file, follow these instructions: Open the file by typing the following command: nano file1 Type the text into the nano editor. Press CTRL+O to save the file. Press CTRL+X to exit the file. Create the Second File Next, create another file called file2 and enter the following text: 10 green bottles standing on a wallIf 1 green bottle should accidentally fallThere'd be 9 green bottles standing on the wall To create this file, follow these instructions: Open the file by typing the following command: nano file2 Type the text into the nano editor. Press CTRL+O to save the file. Press CTRL+X to exit the file. How to Compare Two Files Using Linux The command used in Linux to show the differences between two files is called the diff command. The simplest form of the diff command is as follows: diff file1 file2 If the files are the same, no output displays when using this command. However, as there are differences, the output is similar to the following: 2,4c2,3< 10 green bottles standing on the wall< If one green bottle should accidentally fall< There would be 9 green bottles standing on the wall...> If 1 green bottle should accidentally fall> There'd be 9 green bottles standing on the wall Initially, the output seems confusing, but once you understand the terminology, it is fairly logical. The differences between the two files are as follows: The second file only has three lines. The first file has four. The second file says 1 green bottle on the third line. The first file says one green bottle.The second file says there'd instead of there would on the final line. The output from the diff command shows that between lines two and four of the first file and lines two and three of the second file, there are differences. It then lists the lines from two to four from the first file, followed by the two different lines in the second file. How to Only Show If the Files Are Different If you only want to know if the files are different and you aren't interested in which lines are different, run the following command: diff -q file1 file2 If the files are different, the following displays: Files file1 and file2 differ If the files are the same, nothing displays. How to Show a Message If the Files Are the Same When you run a command, you may want to know that it worked correctly. You want a message to display when you run the diff command whether the files are the same or different To achieve this requirement using the diff command, use the following command: diff -s file1 file2 If the files are the same, this message appears: Files file1 and file2 are identical How to Produce the Differences Side by Side If there are several differences, it can be confusing as to what the differences actually are between the two files. You can change the output of the diff command so that the results are shown side by side. To do this, run the following command: diff -y file1 file2 The output for the file uses the | symbol to show a difference between the two lines, a < to show a line that has been removed, and a > to show a line that has been appended. When you run the command using the demonstration files in this article, all the lines show as different except for the last line of file2, which is shown as deleted. Restrict Column Width When comparing two files side by side, it can be hard to read if the files have several columns of text. To restrict the number of columns, use the following command: diff --width=5 file1 file2 How to Ignore Case Differences When Comparing Files If you want to compare two files, but you don't care whether the case of the letters is the same between the two files, use the following command: diff -i file1 file2 How to Ignore Trailing White Space at the End of a Line If you notice many differences when comparing files and the differences are caused by white space at the end of the lines, prevent these from showing up as changes by running the following command: diff -Z file1 file2 How to Ignore All White Space Differences Between Two Files If you are only interested in the text in a file and you don't care whether there are more spaces in one than the other, use the following command: diff -w file1 file2 How to Ignore Blank Lines When Comparing Two Files If you don't care that one file may have extra blank lines in it, compare the files using the following command: diff -B file1 file2 Summary You can find more information by reading the manual for the diff command. man diff The diff command can be used in its simplest form to show only the differences between two files. You can also use it to create a diff file as part of a patching strategy. Another command you can use to compare files is the cmp command. This compares files byte by byte.