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.

Mowzer
  • 959
  • 2
  • 8
  • 11
  • I'm not sure I understand the question. Any chance you could please provide instructions for generating some files that would work as a test case? – cryptarch Nov 12 '18 at 01:04
  • 1
    @cryptarch: I re-wrote the question for clarity. Can you understand it better now? – Mowzer Nov 12 '18 at 01:12
  • Possible duplicate of [Is there some tool for finding files in one directory but not in the other?](https://unix.stackexchange.com/questions/479947/is-there-some-tool-for-finding-files-in-one-directory-but-not-in-the-other) – αғsнιη Nov 12 '18 at 04:50

1 Answers1

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.

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
  • 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 02: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