0

I have up to terabytes of data (a large number of smaller files) that could contain wrong owner, group or permissions. To repair that we had a script doing

sudo -n chown -R user:group "/path"
sudo -n chmod -R a-rwx,u+rwX,g+rwX "/path"

But I wonder if the performance could be improved? What about running both in background and adding wait in the script? Is this a bad idea for chown and chmod? Do they interfere?

What about using find instead? Is this faster and even correct this way (tests looked like this ain't even working, i.e. there is something wrong with the usage)?

sudo -n find "/path" -type f -exec chmod 660 {} + -exec chown user:group {} +
sudo -n find "/path" -type d -exec chmod 770 {} + -exec chown user:group {} +

What about running this in background and adding wait? Due to the type filter this shouldn't interfere, right? Could this even be improved by filtering for files only deviating from the target configuration?

Maybe it's important to note that this script should be able to run on multiple platforms like Ubuntu (different versions) and Red Hat (different versions) and potentially different file systems, too.

1

1 Answer 1

1

What is exactly the fastest solution in your case depends on your environment, there probably isn't a single best way, and you should run some alternatives to see which performs best.

Edit: As I've learned (thanks @StephenKitt) my assumptions about chmod and find were wrong, and both variants will likely perform well enough, so the xargs shown here is just one more alternative you could try.

I would try find -print0 to identify the files/directories and xargs -0 with chmod and chown in turn. This would of course run find 4 times over your directory tree, but most likely caching will help there a lot. If your find command does not support the -print0 option, you can do without (and omit the -0 option from xargs) but then you should make sure that the files names don't contain line feed characters.

If you want to reduce runs of find you might write its output into a temporary file and use that as input to the two xargs invocations for chown and chmod.

Whether running two in parallel or one after the other is better, and whether checking for files not matching the desired characteristics saves time, is something that you'd need to measure. I'd guess that since this feels like a mostly one-shot operation, it really does not matter much, just get started and get done with it. If the task comes up a second time, you could try an alternative approach if you think that could be better.

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.