Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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".

share|improve this question

3 Answers 3

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.

share|improve this answer
    
add | grep -v "identical" to skip all the file identical messages –  Justin Mclean Mar 11 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 Mar 12 at 14:19
    
Yes omitting -s would be simpler. –  Justin Mclean Mar 12 at 22:45
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.

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.