2

I'm copying one users home directory to be the home directory of user #2, and therefore need to chown the contents to the new user. Running:

find /home/user2 -user user1 -exec chown user2 '{}' \;
find /home/user2 -group user1 -exec chgrp user2 '{}' \;

works ok, but (due to the ./.. files I suppose), somehow the root / was chown'ed to user2. However, /home remained owned by root:root.

How can I be sure what find will do? How can I restrict it from moving up the tree (toward root), even stopping it before it modifies the working directory itself?

superboot
  • 123
  • 5

1 Answers1

2

When I do find /home/$user it doesn't go up the tree.. You can leave the actionable arguments off of the find command to see what will be substituted into the curly braces.

If home is it's own filesystem, and it probably should be, you can use -xdev to not traverse other filesystems. Also, -H tells find not to follow symbolic links.

That said, why not chown -R user2:user2 /home/user2?

Aaron Copley
  • 12,345
  • 5
  • 46
  • 67
  • chown -R user2.user /home/user2 for sure. "." works in place of ":". -R is for recursive. I'd leave the find/chown/chrgrp for more complicated tasks. – dmourati Aug 14 '13 at 18:10
  • Not sure of the difference between `.` and `:`. Can you elaborate? – Aaron Copley Aug 14 '13 at 18:13
  • 1
    @AaronCopley [*"Some older scripts may still use ‘.’ in place of the ‘:’ separator. POSIX 1003.1-2001 (see Standards conformance) does not require support for that, but for backward compatibility GNU chown supports ‘.’ so long as no ambiguity results. New scripts should avoid the use of ‘.’ because it is not portable, and because it has undesirable results if the entire owner‘.’group happens to identify a user whose name contains ‘.’."*](https://www.gnu.org/software/coreutils/manual/coreutils.html#chown-invocation) – jscott Aug 14 '13 at 18:16
  • The error I was thinking about was with `chown user:user ./.*` eg: `mkdir -p /tmp/test/root/home/user` `chown -R root:root /tmp/test/` `chown -R /tmp/test/root/home/user/.*` (an attempt to change all hidden files in a users directory) results in the whole tree (in my test, back to /tmp/) being chown'ed. – superboot Aug 14 '13 at 18:33
  • @dmourati I used the find command so it would only change files owned by the user. Therefore, if files where in the users home directory that were owned by root or other process groups, they would remain so in the new directory. Whereas chown -R would make all files/dirs owned by the user/usergroup. – superboot Aug 14 '13 at 18:39
  • 1
    Why would anything in someone's home directory be owned by anyone else? – Aaron Copley Aug 14 '13 at 19:00
  • [In your test, that's because `.*` matches `..`](http://www.cyberciti.biz/faq/bash-considered-harmful-to-match-dot-files-why/). – Aaron Copley Aug 14 '13 at 19:03
  • I'd do a quick ls -Rl on /home/user2 to make sure there were no weird cases and then just chown as above. – dmourati Aug 14 '13 at 19:23
  • **DON'T** use recursive chmod, or at least you what you're doing, if doing. This might lead to root escalation. For details, see Debian bugs [#889060](https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=889060) and [#889488](https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=889488). – gxx May 26 '18 at 21:00