Re: OFFTOPIC: Re: hardlinks.... sucks... ;-(

Malcolm Beattie (mbeattie@sable.ox.ac.uk)
Wed, 7 Jan 1998 10:37:25 +0000 (GMT)


Michael O'Reilly writes:
> Mitch Davis <mjd#NOSPAM@nsmd.aus.hp.com> writes:
> > Albert D. Cahalan wrote:
> > >
> > > > Try something a bit like one of the following:
> > > >
> > > > find / -user 1234 -print | xargs chown 4321
> > > > find / -user 1234 -print | xargs rm
> > > > find / -user 1234 -exec chown 4321 {} ;
> > > > find / -user 1234 -exec rm {} ;
> > >
> > > It seems a file starting with "-" could cause trouble too.
> > > Maybe a name like "--follow-symlinks" would be interesting.
> >
> > A file containing a space causes xargs to perform in an often-
> > unexpected manner. When in doubt, -exec is the (slower but surer) way
> > to go.
>
> This doesn't really belong here, but the faster, safe way is...
>
> find / -user 1234 -print | perl -nle 'chown 4321, 123, $_'
>
> which only spawns two processes, and doesn't get confused at all about
> spaces or '-'s in filenames.

It does get confused by newline characters in filenames though. You can
get around that by doing

find / -user 1234 -print0 | perl -n0le 'chown 4321, 123, $_'

provided you use GNU find (which Linux dists do) since there
definitely cannot be \0 characters in a filename.

> Still isn't safe from race conditions,
> but that's a different kettle of fish.

True. You need to stat, open, fstat, check dev/inode, fchown to fix
that up perfectly. Perl has "use File::Find; find(\&do_something, "/foo")"
but doesn't have fchown...at the moment. If I get a spare bit of time
I'll see if I can add chown(4321, 123, \*FH) before I get 5.005 out
(not too long now). This is even more off-topic, though.

--Malcolm

-- 
Malcolm Beattie <mbeattie@sable.ox.ac.uk>
Unix Systems Programmer
Oxford University Computing Services