How to Compare Local and Remote Files in Linux
Last Updated :
17 Jun, 2021
In this article, we will discuss how to compare or differentiate between local and remote files in Linux.
Programmers and writers often want to know the difference between two files or two copies of the same file when writing program files or regular text files. The discrepancy between the contents of two Linux machine files is referred to as a diff. A reference to the performance of diff, the well-known Unix command-line file comparison tool, inspired this definition.
diff (short for difference) is a simple and straightforward tool that compares two files line by line and shows the differences between them. It prints the lines that are different. Importantly, if you want the two files to be similar, diff also produces a collection of instructions for how to modify one file to make it identical to the other.
Let's suppose you have two files :
file_local.txt
Hello World..!!
Ubuntu
CentOS
Windows
Hello world..!!
Kubuntu
Ubuntu
CentOS
Windows
Run the following command to compare or identify the differences between two files on separate servers. Remember to change the user and remote host to your own values.
$ ssh user@remote_host "cat /home/root/remote_file" | diff - file_local
Output:
In Linux/Unix-like operating systems, the cat (short for "concatenate") command is one of the most commonly used commands. We can use the cat command to build single or multiple files, display their contents, concatenate files, and redirect output to a terminal or files. You can also use the output redirection option to save the difference between the two files.
$ ssh user@remote_host "cat /home/root/remote_file" | diff - file_local > output_diff.txt
The contents of the diff output.txt file can then be viewed using the cat command.
$ cat output_diff.txt
OR
$ bcat output_diff.txt
Output:
You may also compare or notice the difference between two files on two different remote servers, as shown here:
$ diff <(ssh user@remote_host1 'cat /path/to/file_1') <(ssh user@remote_host2 'cat /path/to/file_2')
Consult the diff man page for more detail, as shown.
$ man diff
If you want to take a more visual approach to your terminal session, there exists a remote file system option and a visual file comparison option in the midnight commander for you. Many Linux systems do not have it installed by default, but it is available in most base repositories. Here are the steps to be taken:
- In the folder containing the file to be compared, run midnight commander (command: mc).
- Build an sftp connection to the other server in the same folder (sftp:/@/) on the other side (for example, the right menu).
- Select/highlight the files to be compared with the ins key. (The tab switches between the left and right panels.)
- Select Compare files from the Command menu.
In most terminal sessions, the midnight commander menu can be accessed by using the mouse. If that doesn't work, F9 will take you to the menu.
Similar Reads
How to Securely Copy Files in Linux | scp Command Secure file transfer is a crucial part of Linux systems administration. Whether moving sensitive files between local machines or transferring data between servers, or you need to move backup files to a remote server, fetch logs from a hosted machine, or sync directories across multiple systems, scp
10 min read
How to Find and Remove Duplicate Files on Linux? Most of us have a habit of downloading many types of stuff (songs, files, etc) from the internet and that is why we may often find we have downloaded the same mp3 files, PDF files, and other extensions. Your disk spaces are unnecessarily wasted by Duplicate files and if you want the same files on a
4 min read
How to Compare Files Line by Line in Linux | diff Command In the world of Linux, managing and comparing files is a common task for system administrators and developers alike. The ability to compare files line by line is crucial for identifying differences, debugging code, and ensuring the integrity of data. One powerful tool that facilitates this process i
9 min read
How to Compare a Local Git Branch with its Remote Branch ? When working with Git, it's often necessary to compare your local branch with its remote counterpart to understand the differences in terms of commits and changes. This helps in keeping your local work in sync with the remote repository and managing potential conflicts. This article will guide you t
3 min read
How to compare two text files in python? Comparing two text files in Python involves checking if their contents match or differ. This process helps you identify whether the files are exactly the same or if there are any changes between them.To download the text files used in this article, click hereUsing hash-based comparisonThis method ca
3 min read
Compare two Files line by line in Python In Python, there are many methods available to this comparison. In this Article, We'll find out how to Compare two different files line by line. Python supports many modules to do so and here we will discuss approaches using its various modules. This article uses two sample files for implementation.
3 min read
Rdiff-backup - A Local and Remote Backup Tool for Linux The Rdiff-backup(Reverse differential backup tool) is a backup tool that backs up one directory to another, either locally or remotely. It is a powerful tool written in Python, that works best with Linux. It works with Windows and Mac OS X as well. Users have reported that it works well on the cross
4 min read
How to Get Last Modified Date of File in Linux? Here we are going to see how to get the last modified date of the file in Linux, sometimes we may require timestamps of the file and apart from this it also ensures that we have the latest version of that file. It can be done in four ways: Using Stat command.Using date command.Using ls -l command.Us
1 min read
How to execute commands remotely using SSH in Linux? Many times users need to work in remote systems. For which they have to log in to the remote server, execute certain commands and come out of that session. Is it possible to perform all these actions locally? Yes, it's possible using ssh client. In this article, we will see different ways of running
2 min read
How to check if a file exists in Scala? When working on software projects it's crucial to check if a file exists before you interact with it in any way such as reading, writing, or modifying it. This practice helps avoid issues that may arise from attempting to handle an existing file. Scala provides methods to perform this check for the
2 min read