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.
1 2 3 4 5 6 7 8 |
#!/bin/sh echo "Enter your first file name" read file1 echo "Enter your second file name" read file2 diff $file1 $file2 |
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.
1 2 3 |
< I need to get the car detailed. --- > I need to get the dog detailed. |
If you want to save the result to a file, you can use below command:
1 |
diff file1.txt file2.txt > result.txt |
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