A hacky solution using diff
, sed
and xargs
:
Test setup:
$ mkdir folderA folderB folderC
$ touch folderA/fi\ le1 folderA/file2 folderA/file3 folderB/file3 folderB/file4
$ diff -rq folderA folderB
Only in folderA: fi le1
Only in folderA: file2
Only in folderB: file4
Now we use sed
to filter all lines starting with string Only in folderA:
and we change this part to folderA/
to form a
relative filename with quotes ('folderA/fi le1'
). This string is then piped to xargs
and used to copy this file to folderC
.
$ diff -rq folderA folderB | sed -n "s/^Only in \(folderA\): \(.*\)/'\1\/\2'/p" | xargs cp -vt folderC
'folderA/fi le1' -> 'folderC/fi le1'
'folderA/file2' -> 'folderC/file2'
The same as above for folderB
:
$ diff -rq folderA folderB | sed -n "s/^Only in \(folderB\): \(.*\)/'\1\/\2'/p" | xargs cp -vt folderC
'folderB/file4' -> 'folderC/file4'
mkdir folderC; cp -p folderA/* folderB/* folderC/
. If there is a penalty, thenrsync
is easy:mkdir folderC; rsync -av folderA/ folderC; rsync -av folderB/ folderC/
.