I have a log file owned by root that I want to copy into a directory owned by another user for him to analyse it (it must be uncompressed first).

Currently I'd have to

# cp log.xz /home/user/Documents
# chown user:user /home/user/Documents/log.xz

Is there a way to issue both commands at the same time, perhaps with one or more cp options?

  • you are asking about running multiple commands at the same time ? – asvignesh Apr 10 '13 at 7:49
  • @Vignesh I'm just asking how to do it the shortest way, perhaps with proper use of pipes ;) – usr-local-ΕΨΗΕΛΩΝ Apr 10 '13 at 8:08

The cp man page does not list any options that would allow you to do this just with the CP command. If you are wanting to just issue a command and "walk away", you can put the 2 commands on 1 line separated by a ; to automatically execute the commands in sequence.

cp log.xz /home/user/Documents ; chown user:user /home/user/Documents/log.xz
| improve this answer | |
  • Though trivial, I asked my question to see if there was any smart one-liner e.g. with proper use of pipes. It probably doesn't exist – usr-local-ΕΨΗΕΛΩΝ Apr 10 '13 at 8:09
  • I don't think its a good solution, but if you are dead set on using pipes, I came up with the following concoction which I suspect will work - "cp -v /from.dir /to/dir 2>/dev/null | cut -f1 -d"'" | cut -c2- | xargs chown user:user " – davidgo Apr 10 '13 at 8:22
  • Do scroll beyond the accepted answer if you really need an answer, folks... – Josiah Yoder Aug 1 '19 at 17:08

You might want to look at install:

install -g user -o user log.xz /home/user/Documents

install has other options suitable for installing. e.g. set mode or preserve directory structure.

| improve this answer | |
  • Nice tip. I tended to think "install" is meant to install things, but its actual summary in the manpage is: "install - copy files and set attributes" – Raúl Salinas-Monteagudo Apr 10 '13 at 13:58
  • @esperanto: The primary use of install is still to install files to the system. Some programs actually override install by modifying the PATH environment variable, so that files are installed elsewhere. Some package builders do that so files are not installed to the current system, but rather saved into a package. – BatchyX Apr 10 '13 at 16:15
  • 1
    It's -g group -o user. -o stands for owner. – Josiah Yoder Aug 1 '19 at 17:14

see map page of cp command

--preserve[=ATTR_LIST] preserve the specified attributes (default: mode,ownership,timestamps), if possible addi- tional attributes: context, links, xattr, all

| improve this answer | |

From cp man page -p option should preserve the attributes including ownership:

-p     same as --preserve=mode,ownership,timestamps
          preserve the specified attributes (default: mode,ownership,timestamps), if possible additional attributes: context, links, xattr, all
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.