How Do I Diff the Output of Two Commands?

In Linux, comparing the outputs of two commands is crucial for ensuring the accuracy and consistency of data and processes. By comparing outputs, one can identify differences, discrepancies, or unexpected results, which are invaluable for troubleshooting, debugging, and verifying the correctness of scripts, programs, or system configurations. It helps detect errors, validate changes, and maintain the integrity of data and operations, thereby enabling efficient problem-solving and quality assurance in the Linux environment.

But How can it be done in Linux? Well, the “diff” command is the catalyst in this regard. The diff command is essential for comparing the outputs of two commands in Linux, enabling users to identify differences or discrepancies between the results quickly. It plays a crucial role in troubleshooting, verifying script correctness, and ensuring data integrity, promoting effective problem-solving and quality assurance.

The commands executed in this post apply to all Linux distributions. However, as a reference, we will be using Ubuntu 22.04.

How Do I Diff the Output of Two Commands?

As discussed earlier, the diff command allows you to differentiate the output of the two commands. Now, there are two methods to do so. Let’s explore both of them one by one.

Method 1: Using the Process substitution

The diff commands with its “-u” option is applied on one command with the process substitution operator “<()” and is joined with the pipe to another command. The syntax of the scenario is as follows:


$ command-1 | diff -u - <(command-2)

The <() construct is process substitution in Bash, which allows you to treat the output of command2 as a temporary file for comparison.


Let’s say we want to compare the output of the two commands, ls and ls -al. The following command will serve the purpose in this scenario:

$ ls | diff -u - <(ls -al)

The output of the diff command provides a line-by-line comparison between two files or inputs, highlighting the added, deleted, or modified lines.

Method 2: Using diff on the txt Files

The outputs of the commands can be redirected to the text files, and then the diff command can be used on those text files. The syntax is as follows:


$ diff -u file-1.txt file-2.txt


From the syntax, it is clear that the diff -u is being applied to two text files. Thus, it is necessary to get the outputs of the commands in the text files first.

Step 1: Put the Output in Text Files

Let’s say we want to compare two commands, i.e., ls and ls -al. First, we will put their outputs in two text files named “ls.txt” and “lsal.txt.”:

$ ls > ls.txt && ls -al > lsal.txt
$ ls

We used the ls command later to check whether the files had been created. The presence of the files can be verified from the above output.

Step 2: Diff the Two Outputs

Now, use the “diff -u” command on the files, i.e., ls.txt and lsal.txt, to get the diff of the outputs stored in the two files:

$ diff -u ls.txt lsal.txt

These are the two methods to diff the outputs of two commands in Linux.

Wrap Up

In Linux, the diff of two commands can be obtained using the diff command. The diff command can be used two ways to get the difference between two outputs. One is to store the output in the text files and then use “diff -u” commands on those text files. The second method is by using the process substitution method using the operator “<().”

Both these methods are explained with syntaxes and suitable examples. Today, you have learned to diff the output of two commands in Linux. For more Linux-based guides, keep visiting Linux Genie.

Print Friendly, PDF & Email