I have two directories, namely "before" and "after" , each of which has it's sub-directories and files.I want to display all the INCREMENTAL changes amongst these two folders from depth 1 level.

What is I mean is to compare the outputs of the following  
commands and display the differences.

1. find before/ -mindepth 1
2. find after/ -mindepth 1

After cimparison I want to display the following:
a."A" before files/folders present ONLY in the after/ 
     hierarchy(these will be deemed as newly added 
b. "D" before files/folders present ONLY in the before/ 
     hierarchy(these will be deemed as deleted
c. "M" before files/folders present in BOTH before/  and 
     after/  hierarchies(these will be deemed as modified
  • What have you tried to solve the problem at hand? Have you tried using diff on the pre/post output of find? That could get you started. – David Dec 7 '18 at 15:38
  • diff -daU 0 before/ after/. But I cannot format the A/D/M indicators – Kumarjit Ghosh Dec 7 '18 at 15:40
  • Not what I meant :). You could do something like: find before -mindepth 1 > state-before, find after -mindepth 1 > state-after and then diff state-before state-after. It's also possible to use special syntax for that (diff <(find ...) <(find ...) IIRC, but I'm not sure about portability). – David Dec 7 '18 at 15:43
  • Not sure about the A/D/M stuff, but "diff -r --brief before after" should give you output that you can then reformat as you see fit. – Lewis M Dec 7 '18 at 16:58

Perhaps even better than diff-ing the output would be this:

#!/bin/sh -eux

find before -mindepth 1 -printf "%p D\n" | cut -d/ -f2- | sort > files-before
find after -mindepth 1 -printf "%p A\n" | cut -d/ -f2- | sort > files-after
join -a2 -a1 files-before files-after | sed 's/D A$/M'


  • we search in before and after for all files,
  • omit the before and after directories themselves (-mindepth 1),
  • append D to files found under before and A to files under after,
  • remove the first component of the path from all found files (the cut),
  • sort the results and store in two separate files.

The last command:

  • pairs lines which speak about the same file (see man join), so that each file (relative to the search dir, because we removed that) appears exactly once, with a D if the file was in before or A if it was in after or D A if it was in both),
  • we include file names which only appear in one of the input files (the -a1 -a2),
  • finally, if a file has both the D and the A flag, we change it to M as requested.
| improve this answer | |
  • Thanks David. I will give this a try and let you know the results. – Kumarjit Ghosh Dec 8 '18 at 15:53

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.