Friday, June 24, 2011

Compare command in case of linux


Found this Good article as a part of book Advance Shell Scripting Guide


To compare 2 files, we use the diff command. How do we compare 2 directories? Specifically, we want to know what files/subdirectories are common, what are only in 1 directory but not the other.

Unix old-timers may remember the dircmp command. Alas, that command is not available in Linux. In Linux, we use the same diff command to compare directories as well as files.

$ diff  ~peter ~george
Only in /home/peter: announce.doc
diff /home/peter/.bashrc /home/george/.bashrc
76,83d72
<
< # Customization by Peter
< export LESS=-m
< export GREP_OPTIONS='--color=always'
< shopt -s histappend
< shopt -s cmdhist
< export PROMPT_COMMAND="history -a;$PROMPT_COMMAND"
< #echo keycode 58 = Escape |loadkeys -
Only in /home/george: .mcoprc
Only in /home/peter: .metacity
Only in /home/george: .newsticker-images
Only in /home/peter: .notifier.conf
Only in /home/george: targets.txt
Only in /home/peter: .xsession-errors


Without any option, diffing 2 directories will tell you which files only exist in 1 directory and not the other, and which are common files. Files that are common in both directories (e.g., .bashrc in the above listing) are diffed to see if and how the file contents differ.

If you are NOT interested in file differences, just add the -q (or--brief) option.

diff -q ~peter ~george  |sort
Files /home/peter/.bashrc and /home/george/.bashrc differ
Only in /home/george: .mcoprc
Only in /home/george: .newsticker-images
Only in /home/george: targets.txt
Only in /home/peter: .metacity
Only in /home/peter: .notifier.conf
Only in /home/peter: .xsession-errors
Only in /home/peter: announce.doc


diff orders its output alphabetically by file/subdirectory name. I prefer to group them by whether they are common, and whether they only exist
in the first or second directory. That is why I piped the output of diff through sort in the above command.

Note that by default diff does not reach into the subdirectories to compare the files and subdirectories at that level. To change its behavior to recursively go down subdirectories, add-r.

diff -qr ~peter ~george  |sort

Thursday, June 23, 2011

Usefull copy commands in Linux


To make a copy of a file in the current directory, enter:
$ cp file.doc file.doc.bak


To copy a file in your current directory into another directory, enter:
$ cp filename /tmp
$ ls /tmp/filename
$ cd /tmp
$ ls
$ rm filename


To copy a file to a new file and preserve the modification date, time, and access control list associated with the source file, enter:
$ cp -p filename /path/to/new/location/myfile

To copy all the files in a directory to a new directory, enter:
$ cp * /home/tom/backup

To copy a directory, including all its files and subdirectories, to another directory, enter (copy directories recursively):
$ cp -R * /home/tom/backup

Script to compare and Print Difference between two Directories recursively


#!/bin/bash

# cmp_dir - program to compare two directories and subDirectiories
# Manish
#Please remove Echo statements i had added them for debugging
# this is the Function part of it
# Expects two argument as Source directory and target Directory now bachalo is folder which does not exhist in destination
# so it says folder does not exhist - just copy  but test is in source and dest so it gets in to that
#**********************************

searchF()
{
file=$1
args=$2
  #echo "in function Search"
# Process each file in directory_1, comparing it to directory_2

for filename in $file/*; do
    fn=$(basename "$filename")
arg="$args/$fn"
   
#echo "Hellooooo"$fn
   
if [ -f "$filename" ]; then # Checking the File and Comparing the Diff
        if [ ! -f "$arg" ]; then
            echo "$fn is missing from $arg"
            let "missing+=1"
#sleep 1
#echo $missing "is the current missing count"
        fi
    elif [ -d "$filename" ]; then # i need to make it recursive here for directories inside-- i think
#echo "HelloBidu"$filename
arg= "$2/$fn"
let "missing+=1"
#sleep 1
#echo $missing "is the current missing count"
searchF $filename $arg #recusrion
    fi
done

}
#****************************
# Main Block Starts Here
# Check for required arguments

missing=0
[ $# -ne 2 ] &&  echo "usage: $0 directory_1 directory_2" 1>&2 &&
    echo "usage: $0 directory_1 directory_2" 1>&2
   
fi

# Make sure both arguments are directories
if [ ! -d $1 ]; then
    echo "$1 is not a directory!" 1>&2
    exit 1
fi

if [ ! -d $2 ]; then
    echo "$2 is not a directory!" 1>&2
    exit 1
fi

#function call
searchF $1 $2
echo "$missing files missing"

Sunday, June 19, 2011

CHM reader on linux UBUNTU

Need Internet Service on your system

Type this sudo apt-get install xchm
and it shoudl do the rest 
Happy Reading :)