11

On a folder with millions of files this can take quite a long time:

chown someuser -Rf /folder_with_lots_of_files/

How can I speed this up if 99.9% of those files already belong to someuser?

rubo77
  • 28,966

1 Answers1

16

use the find command, like:

find /folder_with_lots_of_files -not -user someuser -execdir chown someuser {} \+
gogoud
  • 2,672
  • 3
    how is it different from running chown -R ? isn't find need to read the file owner as well ? – Rabin Nov 24 '15 at 22:57
  • 1
    chown -R blindly attempts to update the ownership of every file. The find approach only issues a chown where truly needed. May be worth trying xargs too, so that a single chown process is issued for, say, every 15 files. – steve Nov 24 '15 at 22:59
  • But it'll still have to stat each file in the tree and that shouldn't be (?) any cheaper that an attempt at chown. I think find with -prune (with -exec ... \+) might help (if there's a criterion for pruning a subtree that the OP can use). – Petr Skocik Nov 24 '15 at 23:02
  • Interesting. This does appear faster. Recursively chowning the kernel tree takes me about 200ms of sys time. The find approach takes about 100. – Petr Skocik Nov 24 '15 at 23:12
  • find has to stat each file to traverse the tree (to distinguish directories), and if the number of chown calls are relatively small, that will run faster. It would not run faster if the fraction were not small, due to overhead of running the -exec to make subprocesses. – Thomas Dickey Nov 24 '15 at 23:16
  • Now I realize chmod has to stat echo node to traverse the tree. That should explain the doubled sys time -- it needs to both stat and chown and both syscalls should be quick, with the syscall overhead dominating. – Petr Skocik Nov 24 '15 at 23:28
  • what if we need to change both user and group? eg. chown user:group? Can we modify this command to accommodate it? – Prasannjeet Singh Jan 23 '23 at 18:40
  • 1
    yes @PrasannjeetSingh, instead of chown someuser above use chown someuser:group. – gogoud Feb 23 '23 at 08:02