Some neat diff tricks in bash

If you like me you have development servers, life servers, backup directories and git – this requires to compare files at times, using diff – this post has some applications of diff I use.

Normally you would run diff like so

diff some_file_1 some_file_2

which would have output like

1d0 
< this line was deleted 
3c2 
> this line was changed
--- 
< to this line 
7a7 
> this was added

where

  • “1d0” are the line numbers where something happened, the “d” means line one was deleted
    “< this line was deleted” is the content that is gone
  • 3c2 line 3 was changed compared to line 2 (do’nt forget line 1 does not exist in the second file.
    and it was changed from “< this line was changed” to “> to this line”
  • 7a7 line 7 was added, “>this was added” to the second file

Now this is all fine for local files, but what if you need to compare a file on the local machine with a file on an external host? Bash comes to help!
You know we an do

ssh EXTERNAL_HOST ls -al /tmp

which will open a secure shell on the host EXTERNAL_HOST and then execute the command ls -al. We can also do

ssh EXTERNAL_HOST cat some_file_2

which will open a secure shell on the host EXTERNAL_HOST and then catalog the file “some_file_2.

Combining the above with diff we can use

diff some_file_1 <(ssh EXTERNAL_HOST cat some_file_2)

which will redirect the output from the ssh command to diff as stdin, diff will then compare the file “some_file_1” with the standard input coming from the redirection (which in fat is some_file_2).

Now

  1. if you have to do this over and over again, I highly recommend to add this as an alias, e.g. for a particular host
  2. if you compare files in your web-desome_filevelopment tree, then the file you are comparing is in the same directory on the external host, so you can utilise this as well.
  3. if you compare file that are at the same location than you can add PWD to it.

You can use the following script in your .bash.alias file to make it very easy:

function ExternalDiff
{
  CURR_PATH=`pwd`
  if [ $# == 0 ]; then
    echo $"you must give me a filename to diff!"
    echo " "
  else
      echo "Running diff ${1} <(ssh EXTERNAL_HOST cat ${CURR_PATH}/${1})"
      diff ${1} <(ssh EXTERNAL_HOST cat ${CURR_PATH}/${1})
  fi
}

All you have to do:

cd /SOME_PATH_WERE_YOUR_WEB_DIRECTORY_IS
ExternalDiff some_file

will now compare the file "some_file" located in the same directory on you EXTERNAL_HOST with the local file of the same name - this saves me heaps of time.

You want it a little more fancy, e.g. in color?
Sure, no problem, install colordiff and replace "diff" command in the script above with "colordiff" and you will get an output simiar to this:


[me /SOME_PATH] #>ExternalDiff courseWidget.php
Running diff courseWidget.php <(ssh EXTERNAL_HOST cat /SOME_PATH/courseWidget.php)
86,87c86,87
< $modules = GetCourseModules( $course_id, $userID, false, true, false ); < // pinf("FOUND MODULES: ".print_r($modules,1));

---

> $modules = GetCourseModules( $course_id, $userID, false, true, true );
> //pinf("FOUND MODULES: ".print_r($modules,1));

[me /SOME_PATH] #>

Leave a Comment

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

You must tick the checkbox for 'I am not a robot' before you can submit your comment!