With GNU find(1), xargs(1), and grep(1):
duplicate the directory structure:
cd src=/path/to/destsource find dest=/other/path/to/srcdestination pat='some_grep_pattern' cd "$dest" find "$src" -type d ! -path /path/to/src"$src" -printf '%P\0' | xargs -0 mkdir -prestore permissions:
rsync -a --existing /path/to/src /path/to/dest/copyingcopy files with the given pattern:
cd /path/to/src"$src" grep -rlZ pattern"$pat" | \ (cd /path/to/dest;"$dest"; \ xargs -0 sh -c ' \ while [ $# -ne 0 ]; do \ grep pattern"$pat" "/path/to"$src/src$1" >"$1"; \ touch -r "$src/$1" >"$1"; \ shift; \ done' sh)restore permissions, assuming Linux, and assuming you don't have filenames with embedded newlines:
cd "$src" getfacl -RPe . | (cd "$dest"; setfacl --restore=-)