How to Compare two Files in Bash Shell

We can make use of the diff command to compare two files in bash shell.

diff – differential file and directory comparator.


Syntax:

diff <file1> <file2>

Example:

bash-3.2$ cat data1.txt 
date,temp
21-07-2023,10.1
22-07-2023,11.2
23-07-2023,11.5

bash-3.2$ cat data2.txt 
date,temp
21-07-2023,10.1
22-07-2023,11.2
23-07-2023,11.5
24-07-2023,11.2

bash-3.2$ diff data2.txt data1.txt 
5d4
< 24-07-2023,11.2
bash-3.2$ 

Screenshot:

Compare two files using Bash Shell

Comments & Discussion

Facing issues? Have questions? Post them here! We're happy to help!