'cp' command: preserve group ownership

Thread Tools Search this Thread
# 1  
'cp' command: preserve group ownership


Here is the scenario:

This file exists, with this ownership and group:
       -rw-rw-r--  1 picard starfleet   4 Jan  3 00:33 myfile.txt

Output of the 'id' command for user picard is:
       $ id picard
       uid=6392(picard) gid=723(human) groups=723(human),918(starfleet)

Output of the 'id' command for user worf is:
       $ id worf
       uid=7413(worf) gid=737(klingon) groups=737(klingon),918(starfleet)

When worf runs:
       $ /bin/cp -p myfile.txt mycopy.txt

the ownership and group look like:
       -rw-rw-r--  1 worf   klingon      4 Jan  3 00:33 mycopy.txt

Both picard and worf belong to starfleet. Even though worf's effective gid isn't "starfleet", worf was expecting to see "starfleet" as the group owner of the mycopy.txt file.

How can worf get the desired results? Is there some other command that should be used instead of 'cp' command to achieve the desired results?
Yes, using "chgrp" to change the group owner works, but is there some single copy-like command (ie: "tar" or "rsync", etc...) that can be used?

# 2  
Since worf is not picard, the attempt (by cp's -p option) to set the user-ID and group-ID (most likely using the chown() system call) will fail. (Note that the -p option attempts to preserve the values of both user-ID and group-ID together; not the two values separately. There shouldn't be any expectation that the group-ID will be preserved if the user-ID can't be preserved.
# 3  
You can use a pax oneliner perhaps (posistion yourself in working directory)
pax -rw -pe -s /myfile.txt/mycopy.txt/g myfile.txt $PWD/

# 4  
Hi Andrewkl.

Just to make sure that this question wasn't answer before on the forum I did a search with google with this statement:

use of "cp -p"

The second search result is a post on linuxquestions.org Smilie


Anyway, the problem has been described by Don Cragun, I'll add that root is the only one that could preserve the GID and UID with cp -p command (I guess).

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Science: Gadgets
Difficulty: Easy
Microphones can be used not only to pick up sound, but also to project sound similar to a speaker.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Tar command to preserve the folder/file arrangement

Hi, I do have question for un tar a file. I have several 'tar'ed files. For example: SRS.tar.bz2. I was trying to untar them in a linux server using the command: tar xvjf SRS.tar.bz2 It worked perfectly. but when I open this file in my mac computer all the files are extracted into a... (7 Replies)
Discussion started by: Lucky Ali
7 Replies

2. UNIX for Dummies Questions & Answers

regarding changing ownership and group

i am able to change the mode using chmod and able to change permission. but i am not able to change group and ownership. getting as invalid can any one help me regarding this . (4 Replies)
Discussion started by: satheeshkr_cse
4 Replies

3. UNIX for Advanced & Expert Users

Permission denied, but user is owner and has group ownership too

Folks, I have a problem with a particular file, that seems to have some kind of lock on it, that takes around 1 hour approx to timeout. I have used lsof and nothing has an open file handle on it, yet I cannot open it. My user/group owns the file and I can create edit/delete files in... (6 Replies)
Discussion started by: scottrus
6 Replies

4. Shell Programming and Scripting

command to group records

hi, i have records like this supplier,product, persons involved 10,150,ravi@yahoo.com 30,200,ravi@yahoo.com 20,111,payal@gmail.com 40,211,ravi@yahoo.com i want to write a command which displays values like this 10,30,40,ravi@yahoo.com for ravi@yahoo.com if there are n number of... (7 Replies)
Discussion started by: trichyselva
7 Replies

5. Linux

failed to preserve ownership operation not permitted

Hi i have written one backup script in linux workstation.The backup script will do zipping files and moved to the backup directory(i,e /net/ts-bcn/sb_backup/sand). #So while moving zipped files to the backup directory i am getting some error like this error:mv:failed to preserve ownership... (2 Replies)
Discussion started by: yogks
2 Replies

6. UNIX for Advanced & Expert Users

command to copy files with original ownership

Hi, I need a command that to copy files from others and to keep files' ownership. Example: I copy file.txt from users "abc" to my local, and file.txt is own by user "abc" in local. Thanks in advance! (3 Replies)
Discussion started by: need_help
3 Replies

7. Shell Programming and Scripting

Rsync - Preserve owner/group with different UID/GID

Dear Folks :-) I want to rsync some files between some servers and preserve files owner and group (not UID or GID), in some machines UID and GID are differents, for example: a) In the rsync server: # stat vbseo.php File: `vbseo.php' Size: 26758 Blocks: 56 IO... (1 Reply)
Discussion started by: Santi
1 Replies

8. Shell Programming and Scripting

command to add users to group

does a command exist to add users to more than one group? i think the usermod command can do this but i'm not really sure. past ways of me doing this has always been to just hack the /etc/group file and put the user in whatever group i need him to be in. however, vi..ing files like /etc/groups... (2 Replies)
Discussion started by: Terrible
2 Replies

9. UNIX for Advanced & Expert Users

File group ownership changing automatically

Hi everyone, Need help with an issue. The group ownership of files on my Solaris system is getting changed automatically. Could someone tell me the reason why? And how could I correct it? One more info- everytime the ownership changes, it changes to "x". Thanks :confused: (1 Reply)
Discussion started by: top_gun
1 Replies

10. UNIX for Dummies Questions & Answers

command help, how do i list the users of a group?

What command allows you to display a list of the userids of all the other users in a group, regardless if they are logged in or not? (3 Replies)
Discussion started by: crabtruck
3 Replies

Featured Tech Videos