WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: How to chown of all files except those owned by root ?  (Read 6660 times)

Offline bigpcman

  • Hero Member
  • *****
  • Posts: 719
How to chown of all files except those owned by root ?
« on: August 25, 2011, 09:00:24 PM »
Is there a simple way to recursively change the owner of all the files in a directory  except for those owned by root?
big pc man

Offline bigpcman

  • Hero Member
  • *****
  • Posts: 719
Re: How to chown of all files except those owned by root ?
« Reply #1 on: August 25, 2011, 09:25:57 PM »
Nervermind, this should work as long as all the non-root files have the same user name.

Code: [Select]
sudo find . -user username -exec chown newuser {} \;
« Last Edit: August 25, 2011, 09:27:40 PM by bigpcman »
big pc man

Offline wget

  • Newbie
  • *
  • Posts: 8
Re: How to chown of all files except those owned by root ?
« Reply #2 on: August 25, 2011, 09:35:40 PM »
Code: [Select]
sudo find . ! -user username -exec chown newuser {} \;just add a "!".
tinycore is so tiny that his busybox did not mention about some useful opt.
or you can try
Code: [Select]
sudo find . -not -user username -exec chown newuser {} \;

Offline maro

  • Hero Member
  • *****
  • Posts: 1228
Re: How to chown of all files except those owned by root ?
« Reply #3 on: August 25, 2011, 09:44:37 PM »
Here are some more generic "musings" about this subject (as I initially understood it to be less specific):

It's pretty simple combining 'find' (and it's '-user ...' test in a negated form) with 'xargs' (i.e. the command as the '-exec' action can be somewhat limiting depending on the intended operation), plus the command you like to apply to all found items, e.g.
Code: [Select]
tc@box:~$ find /dev ! -user root | xargs ls -l
crw--w----    1 tc       staff     136,   0 Aug 25 09:41 /dev/pts/0
crw--w----    1 tc       staff       4,   0 Aug 24 22:23 /dev/tty0
crw-------    1 tc       staff       4,   1 Aug 25 09:39 /dev/tty1
crw--w----    1 tc       staff       4,   2 Aug 24 22:23 /dev/tty2
Which just lists (i.e. via 'ls -l') all objects under '/dev' that are not (hence the '!') owned by 'root'.

A few more points of possible interest: (1) If you want to limit the objects to purely files you'll need to add '-type f' in the tests part of the 'find' command. (2) If the "operation" applied to the items (i.e. 'ls -l' in aboves case) does not handle multiple items at once you'll need to add the '-n 1' parameter to 'xargs'.

So both these points applied to aboves case would lead to
   find /dev ! -user root -type f | xargs -n 1 ls -l

For more details I 'd suggest to "RTFM" (e.g. here and here), bearing in mind that the BusyBox applets of those two commands are not "feature complete". That means that in more "special" cases the 'findutils.tcz' extension will be required. Or in a case of a limitation of just the 'xargs' applet (e.g. not supporting the very useful '-I' option) a shell loop would do the trick (albeit the '-exec' action of 'find' might work as well).
« Last Edit: August 25, 2011, 09:48:50 PM by maro »