Skip to main content
2 of 3
added 21 characters in body
Satō Katsura
  • 13.7k
  • 2
  • 34
  • 52

With GNU find(1), xargs(1), and grep(1):

  • duplicate the directory structure:

      cd /path/to/dest
      find /path/to/src -type d ! -path /path/to/src -printf '%P\0' | xargs -0 mkdir -p
    
  • restore permissions:

      rsync -a --existing /path/to/src /path/to/dest/
    
  • copying files with the given pattern:

      cd /path/to/src
      grep -rlZ pattern | \
          (cd /path/to/dest; \
          xargs -0 sh -c ' \
              while [ $# -ne 0 ]; do \
                  grep pattern "/path/to/src/$1" >"$1"; \
                  shift; \
              done' sh)
    
Satō Katsura
  • 13.7k
  • 2
  • 34
  • 52