4

I want a bash-script to compare OR find differences for multiple files which are stored in different locations:

Files under /tmp/test/

  • A.txt
  • B.txt
  • C.txt

Compare with files under /home/oldtest/

  • A.txt
  • B.txt
  • C.txt

Condition:

  1. File /tmp/test/A.txt compare only with /home/oldtest/A.txt (similarly for other files - B.txt compare with B.txt).

  2. If any changes found: status should be "Modification require in file A.txt".

4 Answers 4

7

diff from GNU diffutils has an option for recursive compare:

diff -qrs /dir/A /dir/B

compares the directories: -q quietly (i.e. no differences are printed, it only reports that files differ), -r recursively (complete trees with all subdirectories), -s reporting identical files.

3
  • add | grep -v "identical" to skip all the file identical messages Commented Mar 11, 2014 at 23:56
  • @JustinMclean it should read | grep -v ' are identical$', unless you want to discard any difference information about files that contain "identical" in their name. Plus, if you omit the -s option, you'll get the same result without the additional process.
    – peterph
    Commented Mar 12, 2014 at 14:19
  • Yes omitting -s would be simpler. Commented Mar 12, 2014 at 22:45
5
cd /tmp/test
for i in *.txt; do diff -q $i /home/oldtest/$i; done

The output will be "Files A.txt and ../home/oldtest/A.txt differ" for each file that has a difference.

2
cd /tmp/test/;
for x in *; do \
  cmp -s "$x" /home/oldtest/"$x" || echo "Modification required in file $x";
done;|more
1

I am not sure if someone will just write you a script but you can get similar functionality using diff. Just use diff -q.

For instance:

$ diff -q .vimrc .bashrc
Files .vimrc and .bashrc differ

If you want to automate that you can, using a script.

For a good reference on learning how to write bash scripts take a look here: http://bash.cyberciti.biz/guide/Main_Page

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .