1

Suppose my build script creates something like:

build
├── [drwxr-xr-x pskocik  pskocik ]  bin
│   └── [-rwxr-xr-x pskocik  pskocik ]  FOO_bin
├── [drwxr-xr-x pskocik  pskocik ]  include
│   ├── [drwxr-xr-x pskocik  pskocik ]  FOO
│   │   ├── [-rw-r--r-- pskocik  pskocik ]  FOO_a.h
│   │   └── [-rw-r--r-- pskocik  pskocik ]  FOO_b.h
│   └── [-rw-r--r-- pskocik  pskocik ]  FOO.h
├── [drwxr-xr-x pskocik  pskocik ]  lib
│   ├── [-rw-r--r-- pskocik  pskocik ]  libFOO.a
│   └── [-rwxr-xr-x pskocik  pskocik ]  libFOO.so
└── [drwxr-xr-x pskocik  pskocik ]  share
    └── [drwxr-xr-x pskocik  pskocik ]  man
        ├── [drwxr-xr-x pskocik  pskocik ]  man1
        │   └── [-rw-r--r-- pskocik  pskocik ]  FOO.1.gz
        └── [drwxr-xr-x pskocik  pskocik ]  man3
            └── [-rw-r--r-- pskocik  pskocik ]  FOO.3.gz

8 directories, 8 files

What's the best way to copy this, say into /usr or /usr/local, so that

  • files get reowned to root:root
  • file modes stay untouched
  • the same applies to directory nodes unless (optional) they already existed (in which case their ownerships and modes remain the same)

?

Petr Skocik
  • 28,816

1 Answers1

2

Use rsync.

rsync -rltpDvh --chown root:root build/ /usr/

That means:

  • r: Recursive
  • l: Copy links
  • t: Preserve timestamps
  • p: Preserve permissions
  • D: --devices
  • v: Verbose
  • h: Human readable progress

If you don't have it installed, use your package management first to install this wonderful tool :)

Related Stuff:

  • This rewrites the the modes in directory nodes, but otherwise it seems to work OK. – Petr Skocik Jun 23 '17 at 11:09
  • What modes are you talking about? Special permisson bits? This was by default on rsync 2.6.7 and older but it was changed - https://linux.die.net/man/1/rsync - If you are talking about ACSs and Xattrs, you will have to add -A and -Xoptions :) –  Jun 23 '17 at 11:34
  • Here's my test script https://pastebin.com/MGvUP9Fb and here's a sample output https://pastebin.com/nFZYQ70f . As you can see, the mode of the preexisting directory at the destination gets changed after the rsync operation. – Petr Skocik Jun 23 '17 at 11:39
  • I dont know how it would behave with the archive option(-athat implies -rlptgoD), that's why i broke down into -rltpDv and removed the group and ownership options. what about if you use rsync -avh --chown root:root build/ /usr/ ? –  Jun 23 '17 at 11:44