Skip to main content
added 169 characters in body
Source Link
Satō Katsura
  • 13.7k
  • 2
  • 34
  • 52

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 -p
    
  • restore 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=-)
    

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)
    

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

  • duplicate the directory structure:

      src=/path/to/source
      dest=/other/path/to/destination
      pat='some_grep_pattern'
    
      cd "$dest"
      find "$src" -type d ! -path "$src" -printf '%P\0' | xargs -0 mkdir -p
    
  • copy files with the given pattern:

      cd "$src"
      grep -rlZ "$pat" | \
          (cd "$dest"; \
          xargs -0 sh -c ' \
              while [ $# -ne 0 ]; do \
                  grep "$pat" "$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=-)
    
added 21 characters in body
Source Link
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)
    
  • restore permissions:

      rsync -a --existing /path/to/src /path/to/dest/
    

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

  • duplicate the directory structure:

      cd /path/to/dest
      find /path/to/src -type d -printf '%P\0' | xargs -0 mkdir -p
    
  • 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)
    
  • restore permissions:

      rsync -a --existing /path/to/src /path/to/dest/
    

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)
    
Source Link
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 -printf '%P\0' | xargs -0 mkdir -p
    
  • 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)
    
  • restore permissions:

      rsync -a --existing /path/to/src /path/to/dest/