1

I have two projects named project1 and project2 with identical file trees as follows.

project1/, project2/
.
├── src
│   ├── index.html
│   ├── main.js
│   ├── normalize.js
│   ├── routes
│   │   ├── index.js
│   │   └── Home
│   │       ├── index.js
│   │       └── assets
│   ├── static
│   ├── store
│   │   ├── createStore.js
│   │   └── reducers.js
│   └── styles
└── project.config.js

Now I want to compare to see if the following files from each tree are identical to each other.

files-to-compare.txt
src/main.js
src/routes/index.js
src/store/reducers.js
project.config.js

Is there any way to accomplish this using a single unix command which leverages the list of files-to-compare.txt without having to do a separate command for each file in the list? If so, how?

Edit: This is a different question than this one because that question asks about comparing all the files in two directories. This question asks about specific files sprinkled across multiple directories. Excluding many of those that would have otherwise been included by the answers to the other question.

2

In a single line -- not a single command -- calling diff once for each:

while IFS= read -r filename; do diff project1/"$filename" project2/"$filename"; done < files-to-compare.txt

This reads the files-to-compare.txt file line-by-line, ensuring that nothing gets in the way of reading the entire line, then calls diff with that filename under each of the project1 and project2 directories.

  • 2
    If it's only required to check whether they're identical (without immediately seeing difference) it might also be acceptable to replace the diff ... command with something like [[ $(md5sum project1/"$filename") = $(md5sum project2/"$filename") ]] || echo $filename differs. – cryptarch Nov 12 '18 at 2:32
  • 1
    @cryptarch: Your comment is very helpful. And it works great. I would also add that if you are on a Mac or OS X, you better substitute md5 for md5sum. – Mowzer Nov 29 '18 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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