Comparing contents two files and show output using shell program

Comparing and finding differences between two files in Linux using Shell Program of content is a common task when working with shell programs with command like diff command, you can easily compare the contents of two files and identify any differences or similarities between them. Whether you’re comparing different versions of a document, checking for changes in code, or troubleshooting data inconsistencies, knowing how to use these shell programs can be a valuable skill for any programmer or system administrator. In this article, we’ll explore how to use diff to compare and find differences between two files of content using shell programs.

Let’s create first file with name file1.txt

I need to buy apples.
I need to run the laundry.
I need to wash the dog.
I need to get the car detailed.

Let’s create second file with name file2.txt to compare.

I need to buy apples.
I need to run the laundry.
I need to wash the dog.
I need to get the dog detailed.

We have created bash file to run in linux with compare.sh. As you can see below file. We have given “echo” command to ask as prompt from user to get the file name. At the end of the file. you can find diff command for comparing the text between two files.

The output will be shown as below. In this output, the c stands for “change”, indicating that there is a difference between the two files. The < symbol indicates the content of file1.txt, while the > symbol indicates the content of file2.txt. The --- line indicates where the change occurs.

If you want to save the result to a file, you can use below command:

This will save the output of the diff command to a file called result.txt.

Please check out another article  – CONVERT ALL THE CONTENTS INTO THE UPPERCASE IN LINUX USING SHELL PROGRAM

Scroll to Top