24

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?

2
  • you are asking about running multiple commands at the same time ?
    – asvignesh
    Apr 10, 2013 at 7:49
  • @Vignesh I'm just asking how to do it the shortest way, perhaps with proper use of pipes ;) Apr 10, 2013 at 8:08

4 Answers 4

9

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
3
  • 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 Apr 10, 2013 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, 2013 at 8:22
  • 1
    Do scroll beyond the accepted answer if you really need an answer, folks... Aug 1, 2019 at 17:08
48

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.

3
  • 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" Apr 10, 2013 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, 2013 at 16:15
  • 2
    It's -g group -o user. -o stands for owner. Aug 1, 2019 at 17:14
1

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

0

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

-p     same as --preserve=mode,ownership,timestamps
--preserve[=ATTR_LIST]
          preserve the specified attributes (default: mode,ownership,timestamps), if possible additional attributes: context, links, xattr, all

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .