5

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.

3 Answers3

4

You can take ownership like this:

$ mv A A.old
$ mkdir A
$ mv A.old/* A.old/.[!.]* A.old/..?* A/
$ rmdir A.old
maxschlepzig
  • 57,532
  • Great, thanks! Do you mind elaborating on why this approach would work, and why it would not work with hidden files? – Amelio Vazquez-Reina 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 'SO- stop being evil' Jul 30 '12 at 00:24
  • I'm curious why you expect the mv to have any effect on ownership – Alan Curry Jul 30 '12 at 03: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 06: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 06:48
  • Thanks. Sorry for the confusion. I meant recursive ownership. Does this solution still apply? – Amelio Vazquez-Reina Jul 31 '12 at 12:35
  • Thanks @Gilles. From what I understand, this would not work recursively (i.e. transfer owrenship of all folders and subfolders of A) is that right? I just want to make sure I am not missing anything. – Amelio Vazquez-Reina Aug 02 '12 at 16:27
  • @roseck Indeed, that only takes ownership of the directory A itself. You can apply this recursively to take ownership of all directories (for files, this is probably not necessary, you'll take ownership progressively as you rewrite them). You can use find -type d to do this recursively… But there's surely a better way. – Gilles 'SO- stop being evil' Aug 02 '12 at 16:44
1

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
0

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...

Alan Curry
  • 2,274