Linux Unix help !!

"Give respect to Time, One day at right Time, Time will respect You"
Showing posts with label Bash Scripting. Show all posts
Showing posts with label Bash Scripting. Show all posts

Monday, May 14, 2012

Diff file1 file2


Many time we come across situation that we want exact diff between two files ..
There are various Linux command which we can use like

# diff
# cmp

EG:
# cat  1
1
2
3
4
5
6
# cat  2
2
3
6
7
8

### Using diff
# diff -iw 1 2
1d0
< 1
4,5d2
< 4
< 5
6a4,5
> 7
> 8


Expln:
1d0< 1
- Add "1" at line 1 of file 2
4,5d2
< 4
< 5

- Add  "4" at line 4 of file 2
- Add  "5" at line 5 of file 2
6a4,5
> 7
> 8

- Add  "7" at line 7 of file 2
- Add  "8" at line 8 of file 2

-- Actual count as line 0 - N


Here it's tough to found whats mean of above if you are using it first time, What we expect is I want only those line which are missing in file 2 .

So have written a small PerlScript to achieve so, it's possible with BashScript too, but to parse a large file BashScript will take more time .


Script : diff.pl

#!/usr/bin/perl
# Written By - Shirish
#### perl  diff.pl file1 file2  OUTPUT
#### Will give you exact diff between file1 and file2 in OUTPUT file
##### Only lines that are in file1 but missing in file2
#################
#open a, "<filea";
#open b, "<fileb";
$x = $ARGV[0];
$y = $ARGV[1];
$z = $ARGV[2];
open a, "< $x";
open b, "< $y";
open (OP, ">$z") || die ("Unable to create Report");
local $/;
my @a = split /\n/, <a>;
my @b = split /\n/, <b>;
my %b = map { $_ => 1 } @b; # Make hash of B
my @res = grep { !defined $b{$_} } @a; # Everything in A not in B
print OP join "\n", @res;
#print join "\n", @res;
print "\n";

## How to use
## What's in file 1 which are absent in file 2
# perl diff.pl 1 2 1-2 
# cat 1-2
1
4
5

## What's in file 2 which are absent in file 1
# perl diff.pl 2 1 2-1
# cat 2-1
7
8




> Shirish Shukla




difference between two file linux, unix, find diff of two files, command diff, perl script to find diff of two files, compare two files, linux, linuxva, shirish, shukla

Sunday, March 11, 2012

backup using script imp files linux

Many time we come across situation to take a backup of file daily/weekly or sometime every hour .

Yes it may be that your filesystem are auto backuped by your backup tools as Snapshot/Veritas netbackup etc.. but still we always come across situation to that we want our some critical file (/etc/passwd, /etc/httpd/conf/httpd.conf etc..)  that had changed 1 hour back .

Below a small and simple script that can help us a way .. just schedule this in your crontab .. as per your requirement .

## MyBackup.sh
#!/bin/bash
#Shirish Shukla
#######################################
## Files to be backuped
Sources="/etc/passwd, /etc/group, /etc/shadow"
## Backup Save at Target:
Targetpath=/usr/Backup
#######################################
if [ ! -d $Target-path ]
then
    mkdir -p $Targetpath
fi
#######################################
create=`date +%a-%d-%b-%Y-AT:%H:%M`
delete=`date --date '1 month ago' +%a-%d-%b-%Y`
#######################################
for fles in `echo $Sources`
do
   fle=`echo $fles | sed  "s/,//g"`
    if [ ! -e $fle ]
    then
          echo "file: $fle not EXIST"
    fi
    lst=` echo $fle | awk -F"/" '{print $NF}'`
    tar -jcf $Targetpath/$lst-$create.bz2 $fle
done
#######################################
## Now delete 1 month old backups if exists any
rm -fr  $Targetpath/*$delete*
#######################################

## Check Backup
# ls -lrt  /usr/Backup/
-rw-r--r-- 1 root root 500 Feb 11 11:47 shadow-Sat-11-Feb-2012-AT:11:47.bz2
-rw-r--r-- 1 root root 938 Feb 11 11:47 passwd-Sat-11-Feb-2012-AT:11:47.bz2
-rw-r--r-- 1 root root 500 Feb 11 11:47 group-Sat-11-Feb-2012-AT:11:47.bz2


## Schedule crontab to take it 2 hour
# chmod 755 /root/MyBackup.sh
# crontab -e
01  */2 * * *  /root/MyBackup.sh


> Shirish Shukla

Thursday, October 28, 2010

my scripts

==========> Backup Script
#!/bin/bash
# Shirish Shukla backup script
# source-> /usr/rnd/*
# target-> /shirishva/backup/abcd-...
#
#------------ Backup string create: abcd-Wed-Oct-27-22:31 format

create=$(echo abcd-`date +%a-%b-%d-%H:%M`)

tar -jcf /shirishva/backup/$create.bz2 /usr/rnd/*

#------------------- delete 3 Hour old backup data del->string

t1=$(date --date='3 hour ago')

t2=$(echo $t1 | sed -e "s/[:]/ /g")

array=( `echo $t2` )

del=$(echo abcd-${array[0]}-${array[1]}-${array[2]}-${array[3]}:${array[4]})

rm -fr /shirishva/backup/$del.bz2

#---------------------------------

Followers

Pls LIKE my Story !!!