Rectangle 27 0

A one liner solution using awk and diff

LANG=C diff -aqr /tmp/d1 /tmp/d2 |
    awk -F'[ :]' '/^Only in/{system("cp -a "$3"/"$NF " /tmp/d3/")}'
  • LANG=C force diff output to be in English for erliable parsing
  • diff -aqr /tmp/d1 /tmp/d2 will display the files that are missing or different from both dirs recursively
awk
  • -F'[ :]' means : spliting columns on spaces and :
  • '/^Only in/is like agrep`, and if the pattern is found, we execute :
system("cp -a "$3"/"$NF " /tmp/d3/")
  • the script don't handle file/dir with space in names, this require extra processing, but you have a good start now

This one is the shortest, could you provide a short explanation on what awk does here?

shell - Bash script that copies the differences in two directories to ...

bash shell