Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Consider the following scenario:

  1. I have rwx access to a directory as a member of the group id of the directory.
  2. The system admin does not let users run chown (see this thread for details)

How can I take recursive ownership of the directory?

I believe I can do the following, assuming that I want to own A

cp -R A B
rm -R A
mv B A

but this is tedious and can require a large amount of space if A is large.

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

You only really need to take ownership of directories. Ordinary files will take care of themselves the next time you modify them, symbolic links and pipes don't matter, and I'm going to assume there are no devices or other exotic types.

You can make a recursive copy of the directories, but make hard links from the regular files instead of copying them. With GNU coreutils (Linux, Cygwin):

cp -al A B

Every regular file A/dir/file is hard-linked as B/dir/file. You can then remove the source tree.

If you don't have GNU coreutils, you can use rsync instead:

cd A
rsync -a --link-dest=$PWD . ../B

To make sure that deleting A will not actually remove any file, check that all regular files have a hard link count of at least 2 — the following command should not output anything:

find A -type f -links 1
share|improve this answer
add comment

You can take ownership like this:

$ mv A A.old
$ mkdir A
$ mv A.old/* A.old/.[!.]* A.old/..?* A/
$ rmdir A.old
share|improve this answer
    
Great, thanks! Do you mind elaborating on why this approach would work, and why it would not work with hidden files? –  user815423426 Jul 29 '12 at 22:26
1  
@roseck The third command skips hidden files. It's not a lot more work to make it include them, see my edit. –  Gilles Jul 30 '12 at 0:24
    
I'm curious why you expect the mv to have any effect on ownership –  Alan Curry Jul 30 '12 at 3:26
    
@AlanCurry the mv does not have any effect on the ownership but the mkdir A has. The new created directory will belong to the user –  Ulrich Dangel Jul 30 '12 at 6:34
    
@UlrichDangel oh! I was interpreting the phrase "take ownership of the directory" as implying that recursive chowning was desired. –  Alan Curry Jul 30 '12 at 6:48
show 3 more comments

Any way to do this that doesn't involve copying is going to run into the no-chown rule. Ask root nicely to do it for you.

If you're ambitious you could write a new chown-like utility, designed for setuid-root installation, that does its own security checks and decides based on a looser rule whether a chown should be allowed.

If it's mainly about the disk space, you only need a tool that interleaves the cp with the rm...

share|improve this answer
    
AFAIK you can do sparse copies if the file has many zeroes: cp --sparse=always –  Janus Troelsen Jul 30 '12 at 20:16
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.