How to Compare Local and Remote Files in Linux

In this article, we will show how to compare or find the difference between local and remote files in Linux. In a recent post, we reviewed 9 best file comparison and difference (Diff) tools for Linux. One of the tools we covered was diff.

diff (short for difference) is a simple and easy to use tool which analyzes two files and displays the differences in the files by comparing the files line by line. It prints the lines that are different. Importantly, if you want the two files to be identical to each other, diff also outputs a set of useful instructions on how to change one file to make it identical to the second file.

To compare or find the difference between two files on different servers, run the following command. Remember to replace the user and remote host with your actual parameters.

$ ssh [email protected] "cat /home/root/file_remote" | diff  - file_local 
Compare Two Files on Different Servers

Compare Two Files on Different Servers

Note that you can also save the difference between the two files to a file, using the output redirection feature. For example:

$ ssh [email protected] "cat /home/root/file_remote" | diff  -  file_local > diff_output.txt

Then use a cat command to view the contents of the diff_output.txt file.

$ cat diff_output.txt
OR
$ bcat diff_output.txt

In addition, you can also compare or find the difference between two files on two remote servers, as shown:

$ diff <(ssh [email protected] 'cat /path/to/file1') <(ssh [email protected] 'cat /path/to/file2')

For more information, consult the diff man page as shown.

$ man diff

Also, check out:

  1. How to Find Difference Between Two Directories Using Diff and Meld Tools
  2. Linux sdiff Command Examples for Linux Newbies
  3. A – Z Linux Commands – Overview with Examples

That’s it for now! In this article, we have shown how to compare or find the difference between two files on different servers. Share your thoughts with us or ask questions via the feedback form below.

Best Affordable Linux and WordPress Services For Your Business
Outsource Your Linux and WordPress Project and Get it Promptly Completed Remotely and Delivered Online.

If You Appreciate What We Do Here On TecMint, You Should Consider:

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Get your own self-hosted blog with a Free Domain at ($3.45/month).
  4. Become a Supporter - Make a contribution via PayPal
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux courses

We are thankful for your never ending support.

Aaron Kili

Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

Got something to say? Join the discussion.

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.