Often system administrators and software developers need to compare files on their system. One of the most common tools used for this purpose is called diff, installed by default on almost every Linux system. Typically people use diff to compare two or more local files on their system. But sometimes you may need to compare local & remote files in Linux. In this article, we will learn how to compare local & remote files in Linux.
diff is a simple and powerful tool that allows you to compare two or more files line by line, and lists the differences between them. It also lists changes to be made to make these files identical.
How to Compare Local & Remote Files in Linux
Here are the steps to compare local & remote files in Linux. To compare local file with remote file we will use diff and ssh together. The ssh utility is used to connect to remote file. So you need to have a working ssh username & password for access to your remote file. Also your SSH user needs to have read access to your remote file. Otherwise, you will get an error. Here is the basic command to compare local file with remote file.
$ ssh user@remote-host "cat /path/to/remote/file" | diff - /path/to/local/file
In the above command, we use SSH to connect to remote server where your remote file is located. Replace user and remote-host above with the SSH username and remote server IP address respectively. Then we issue cat command to be run on remote server, on successful remote access. For cat command, we specify the file path of your remote file.
This entire combination of SSH & cat command will basically allow you to connect to remote server and display the contents of remote file on your local system. We pipe this output to diff command. We also specify the local file’s path to diff command after hyphen. So the output of cat command is used as first file and the local file is used as second file, and a comparison is made between them.
Here is an example of the above command.
$ ssh firstname.lastname@example.org "cat /home/root/data.txt" | diff - /home/ubuntu/data.txt
The above command will display all differences between two files on your terminal.
Please note, when you run the above command, you will be asked to enter SSH password for authentication. Command execution will proceed further only after successful authentication.
If you want to store the output to another file, you can do so using redirection ‘>’ operator.
$ ssh user@remote-host "cat /home/root/file_remote" | diff - file_local > diff_output.txt
If both your files are remote to your system, and you want to compare two remote files, you can use the following command for this purpose. Replace user1 & remote-host1 with the username and remote host IP address to first remote server. Replace user2 & remote-host2 with the username and remote host IP address to second remote server.
$ diff <(ssh user1@remote-host1 'cat /path/to/file1') <(ssh user2@remote-host2 'cat /path/to/file2')
Please note, for the above command to work properly, you need to have remote access to both remote files, that is, SSH usernames & passwords to both remote servers and read access to both files.
Please note, when you run the above command, you will be asked to enter two SSH passwords for authentication, one for each remote server.
In this article, we have learnt how to compare local & remote files in Linux, using diff tool.
If you don’t want to be asked passwords when you enter the above commands, then you need to disable password authentication on your SSH servers, and go for key-based authentication.
How to Generate Strong Pre Shared Key in Linux
How to Run Command After Certain Time in Linux
Timeout Command in Linux
How to Set SSH Warning Message in Linux
How to Remove Specific Item From JS Array