Exploring various methods to compare text files in Linux via command line is invaluable. This article focuses on seven different commands that assist in this task and offers insights on how to decipher the outcomes.
To facilitate a better understanding of the commands provided, let’s consider that file1 and file2 include the following content:
Meanwhile, file3 includes:
The diff command is among the simplest to use for identifying differences between two text files. The initial example below showcases a comparison between two identical files, hence producing no output. Conversely, the subsequent example illustrates the comparison between files with distinct contents. The < and > symbols denote the first and second files respectively, with three dashes highlighting the divergent content. Lines that are same in these files are not shown – for instance, the “and milk duds!!” line is absent.
The comm command is used for comparing two text files, requiring the contents to be sorted beforehand. If the files are not sorted, errors will occur during comparison. Once sorted, the command displays output in columns: exclusive lines from the first file appear in the first column, exclusive lines from the second in the second column, and common lines in the third column.
The cmp command, when run without options, simply verifies that the files are different and indicates where the first difference occurs, such as at the 12th character of the first line.
Like the diff command, the diff3 command compares three files. It highlights differences, with an output format that distinctly points out discrepancies among the files. For instance, “====3” indicates a divergence in the third file, followed by the specific differences between the first two files and the third.
The sdiff command is used for comparing files side by side. The initial example demonstrates identical file contents. However, in the subsequent illustration, only the final line matches between the two files, with an absence of the vertical bar that highlights discrepancies in earlier lines.
The colordiff command is similar to the diff command but enhances visibility by color-coding differing content. The first example shown produces no output as the files compared are similar. The second example, however, showcases the differences, color-coding them in red and green as would appear on a computer screen.
The wdiff command, when used, presents the contents of the files being compared in a single document if they are the same. If differences exist, it marks these using square brackets, plus and minus signs to show their locations, as illustrated in the second example provided.
This post’s brief overview of commands shouldn’t suggest that these are the only options available. Executing a command like wdiff –help will display a complete list of options for that command.
Welcome to DediRock, your trusted partner in high-performance hosting solutions. At DediRock, we specialize in providing dedicated servers, VPS hosting, and cloud services tailored to meet the unique needs of businesses and individuals alike. Our mission is to deliver reliable, scalable, and secure hosting solutions that empower our clients to achieve their digital goals. With a commitment to exceptional customer support, cutting-edge technology, and robust infrastructure, DediRock stands out as a leader in the hosting industry. Join us and experience the difference that dedicated service and unwavering reliability can make for your online presence. Launch our website.