Tux

...making Linux just a little more fun!

Efficient chmod/chown

Mike Orr [sluggoster at gmail.com]


Thu, 15 Oct 2009 11:18:15 -0700

Is there a version of chmod/chown that recursively changes files only if they're different from the specification? The stock version changes them unconditionally, which updates their 'ctime' and thus makes 'rsync' transfer the inode properties even if there's no real change. I could write a Python version, but I was hoping there might be a C version somewhere that would be more efficient with millions of filenames.

-- 
Mike Orr <sluggoster@gmail.com>


Top    Back


Anderson Silva [afsilva at gmail.com]


Thu, 15 Oct 2009 14:33:16 -0400

On Thu, Oct 15, 2009 at 2:18 PM, Mike Orr <sluggoster@gmail.com> wrote:

> Is there a version of chmod/chown that recursively changes files only
> if they're different from the specification? The stock version
> changes them unconditionally, which updates their 'ctime' and thus
> makes 'rsync' transfer the inode properties even if there's no real
> change. I could write a Python version, but I was hoping there might
> be a C version somewhere that would be more efficient with millions of
> filenames.
>

By 'specification' do you mean a RPM spec file?

rpm -V package to see what has changed
rpm --setperms and --setugids

would do the trick.

AS

-- 
https://www.the-silvas.com


Top    Back


Deividson Okopnik [deivid.okop at gmail.com]


Thu, 15 Oct 2009 15:43:58 -0300

2009/10/15 Anderson Silva <afsilva@gmail.com>:

> On Thu, Oct 15, 2009 at 2:18 PM, Mike Orr <sluggoster@gmail.com> wrote:
> By 'specification' do you mean a RPM spec file?
>

I guess he meant from what you specify - eg, if you chmod 777 a file, and its already 777, it should skip the file, not set the permissions again.


Top    Back


Thomas Adam [thomas.adam22 at gmail.com]


Thu, 15 Oct 2009 19:59:00 +0100

On Thu, Oct 15, 2009 at 11:18:15AM -0700, Mike Orr wrote:

> Is there a version of chmod/chown that recursively changes files only
> if they're different from the specification?  The stock version
> changes them unconditionally, which updates their 'ctime' and thus
> makes 'rsync' transfer the inode properties even if there's no real
> change.  I could write a Python version, but I was hoping there might
> be a C version somewhere that would be more efficient with millions of
> filenames.

Yes, use find(1) to do this.

-- Thomas Adam

-- 
"It was the cruelest game I've ever played and it's played inside my head."
-- "Hush The Warmth", Gorky's Zygotic Mynci.


Top    Back


Anderson Silva [afsilva at gmail.com]


Thu, 15 Oct 2009 15:42:01 -0400

On Thu, Oct 15, 2009 at 2:59 PM, Thomas Adam <thomas.adam22@gmail.com> wrote:

> On Thu, Oct 15, 2009 at 11:18:15AM -0700, Mike Orr wrote:
>> Is there a version of chmod/chown that recursively changes files only
>> if they're different from the specification? The stock version
>> changes them unconditionally, which updates their 'ctime' and thus
>> makes 'rsync' transfer the inode properties even if there's no real
>> change. I could write a Python version, but I was hoping there might
>> be a C version somewhere that would be more efficient with millions of
>> filenames.
>
> Yes, use find(1) to do this.

+1 on that...

find . -user xuser1 -exec chown -R user2 {} \;

or

find . -perm -220

etc...

-- 
https://www.the-silvas.com


Top    Back


Thomas Adam [thomas.adam22 at gmail.com]


Thu, 15 Oct 2009 20:50:02 +0100

On Thu, Oct 15, 2009 at 03:42:01PM -0400, Anderson Silva wrote:

> On Thu, Oct 15, 2009 at 2:59 PM, Thomas Adam <thomas.adam22@gmail.com> wrote:
> > On Thu, Oct 15, 2009 at 11:18:15AM -0700, Mike Orr wrote:
> >> Is there a version of chmod/chown that recursively changes files only
> >> if they're different from the specification? ?The stock version
> >> changes them unconditionally, which updates their 'ctime' and thus
> >> makes 'rsync' transfer the inode properties even if there's no real
> >> change. ?I could write a Python version, but I was hoping there might
> >> be a C version somewhere that would be more efficient with millions of
> >> filenames.
> >
> > Yes, use find(1) to do this.
> >
> +1 on that...
> 
> find . -user xuser1 -exec chown -R user2 {} \;

No. Not like that.

> or
> 
> find . -perm -220

... and yes, exactly like that, if only because find will call stat(2) directly to determine this, and if I am reading the source correctly for find, it won't touch the file if it's matched the permission. (It's unclear from Mike whether "-perm /200" is more applicable here or not, but it's academic at this point.)

-- Thomas Adam

-- 
"It was the cruelest game I've ever played and it's played inside my head."
-- "Hush The Warmth", Gorky's Zygotic Mynci.


Top    Back


Mike Orr [sluggoster at gmail.com]


Thu, 15 Oct 2009 12:52:09 -0700

On Thu, Oct 15, 2009 at 12:42 PM, Anderson Silva <afsilva@gmail.com> wrote:

> On Thu, Oct 15, 2009 at 2:59 PM, Thomas Adam <thomas.adam22@gmail.com> wrote:
>> On Thu, Oct 15, 2009 at 11:18:15AM -0700, Mike Orr wrote:
>>> Is there a version of chmod/chown that recursively changes files only
>>> if they're different from the specification? The stock version
>>> changes them unconditionally, which updates their 'ctime' and thus
>>> makes 'rsync' transfer the inode properties even if there's no real
>>> change. I could write a Python version, but I was hoping there might
>>> be a C version somewhere that would be more efficient with millions of
>>> filenames.
>>
>> Yes, use find(1) to do this.
>
> +1 on that...
>
> find . -user xuser1 -exec chown -R user2 {} \;
>
> or
>
> find . -perm -220
>
> etc...

Thanks. I'll need to make it a bit more elaborate than that though because I want to avoid '-R' completely. What I really want to do is find files that don't have a 'user1:group1:mode1' combination and change those. I guess I can do several finds over the same tree, or make a Python program to set up the finds (which might be more flexible anyway).

By the way, hi Anderson from long ago when I used to edit your articles. :) Thanks for your help.

-- 
Mike Orr <sluggoster@gmail.com>


Top    Back


Thomas Adam [thomas.adam22 at gmail.com]


Thu, 15 Oct 2009 21:00:25 +0100

On Thu, Oct 15, 2009 at 12:52:09PM -0700, Mike Orr wrote:

> On Thu, Oct 15, 2009 at 12:42 PM, Anderson Silva <afsilva@gmail.com> wrote:
> >
> > find . -user xuser1 -exec chown -R user2 {} \;
> >
> > or
> >
> > find . -perm -220
> >
> > etc...
> 
> Thanks.  I'll need to make it a bit more elaborate than that though
> because I want to avoid '-R' completely.  What I really want to do is
> find files that don't have a 'user1:group1:mode1' combination and
> change those.  I guess I can do several finds over the same tree, or

find /foo -type f -not -perm -something ...
 
> make a Python program to set up the finds (which might be more
> flexible anyway).

No need. I guarantee you that find can do it all for you.

You find some of the techniques used here applicable:

https://linuxgazette.net/111/tag/4.html

-- Thomas Adam

-- 
"It was the cruelest game I've ever played and it's played inside my head."
-- "Hush The Warmth", Gorky's Zygotic Mynci.


Top    Back