Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
    
you are asking about running multiple commands at the same time ? –  Vignesh Apr 10 '13 at 7:49
    
@Vignesh I'm just asking how to do it the shortest way, perhaps with proper use of pipes ;) –  djechelon Apr 10 '13 at 8:08
add comment

3 Answers

up vote 2 down vote accepted

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
share|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 –  djechelon 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
add comment

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.

share|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" –  esperanto 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
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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