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

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap