> For the complete documentation index, see [llms.txt](https://cajac.gitbook.io/ctf-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cajac.gitbook.io/ctf-notes/misc/text-processing/comparing-files-line-by-line.md).

# Comparing files line by line

## Comparing files with comm

The `comm` command compares two text files, displaying the lines that are unique to each one, as well as the lines they have in common. It outputs three columns:

* the first contains lines that are unique to the first file or argument;
* the second contains lines that are unique to the second file or argument;
* and the third column contains lines that are shared by both files.

The -n option, where "n" is either 1, 2, or 3, can be used to suppress one or more columns, depending on the need.

```bash
kali@kali:~$ cat scan-a.txt
192.168.1.1
192.168.1.2
192.168.1.3
192.168.1.4
192.168.1.5

kali@kali:~$ cat scan-b.txt
192.168.1.1
192.168.1.3
192.168.1.4
192.168.1.5
192.168.1.6

kali@kali:~$ comm scan-a.txt scan-b.txt
                                192.168.1.1
192.168.1.2
                                192.168.1.3
                                192.168.1.4
                                192.168.1.5
              192.168.1.6

kali@kali:~$ comm -12 scan-a.txt scan-b.txt
192.168.1.1
192.168.1.3
192.168.1.4
192.168.1.5
```

## Comparing files with diff

The `diff` command is used to detect differences between files, similar to `comm`. However, the `diff` command is much more complex and supports many output formats. Two of the most popular formats include the context format (-c) and the unified format (-u).&#x20;

The following example demonstrates the difference between the two formats.

```bash
kali@kali:~$ diff -c scan-a.txt scan-b.txt
*** scan-a.txt    2018-02-07 14:46:21.557861848 -0700
--- scan-b.txt    2018-02-07 14:46:44.275002421 -0700
***************
*** 1,5 ****
  192.168.1.1
- 192.168.1.2
  192.168.1.3
  192.168.1.4
  192.168.1.5
--- 1,5 ----
  192.168.1.1
  192.168.1.3
  192.168.1.4
  192.168.1.5
+ 192.168.1.6

kali@kali:~$ diff -u scan-a.txt scan-b.txt
--- scan-a.txt    2018-02-07 14:46:21.557861848 -0700
+++ scan-b.txt    2018-02-07 14:46:44.275002421 -0700
@@ -1,5 +1,5 @@
 192.168.1.1
-192.168.1.2
 192.168.1.3
 192.168.1.4
 192.168.1.5
+192.168.1.6
```

The output uses the `-` indicator to show that the line appears in the first file, but not in the second. Conversely, the `+` indicator shows that the line appears in the second file, but not in the first.

The most notable difference between the two formats is that the unified format does not show lines that match between files, making the resulting output shorter. The indicators have identical meanings in both formats.

## Resources

**comm** - Linux manual page: <https://man7.org/linux/man-pages/man1/comm.1.html>

**diff** - Linux manual page: <https://man7.org/linux/man-pages/man1/diff.1.html>
