Operating System - HP-UX
1837239 Members
2012 Online
110115 Solutions
New Discussion

finding filesystems /dirs/files belonging to a user

 
SOLVED
Go to solution
Chern Jian Leaw
Regular Advisor

finding filesystems /dirs/files belonging to a user

HI,

I'm trying to find directories and files within a filesystem belonging to a particular user and change their uids.

The problem is there're many other filesystems containing dirs/files which may belong to a particular user.

I did the following below:
#cat findUser.sh
for i in `cat $1`
do
find $i -user joe_black -exec chown kevin_lee $i {}\;
done

$1 is the lists of filesystems to be searched for that particular user.

A sample of $1 would be:
#cat file1.txt
/fs1/test_tools/
/fs2/auto_cad/lib
/fs3/regression_test/
.... (the list continues ...)

I've tried breaking the list and placing smaller numbers of filesystem lists into a other files. But the search time is simply to slow given the enormity of the filesystems structure.

Could some one help me out on providing more efficient ways or some scripts to improve the efficiency of this task?

Thanks.
10 REPLIES 10
Michael Tully
Honored Contributor
Solution

Re: finding filesystems /dirs/files belonging to a user

Change your find command line to something like this:

find $i -user joe_black | xargs chown kevin_lee

xargs is far more efficient at doing these type of tasks.
Anyone for a Mutiny ?
Ralph Grothe
Honored Contributor

Re: finding filesystems /dirs/files belonging to a user

I find the loop not utterly performant because you invoke a new find process for every filesystem placed in $i.
I would rather do it like this (provided the file with the filesystems isn't too big ;-)

find $(cat /file/with/filesystems) -user joe_black -print | xargs chhown ...
Madness, thy name is system administration
Chern Jian Leaw
Regular Advisor

Re: finding filesystems /dirs/files belonging to a user

Michael, Ralph,

There're lots and lots of sub-directories and files within each of the searched filesystems.

Ralph,
Do you mean to say I should just eliminate the for-loop and call:

find $(cat /filesystems/) -user joe_black |xargs chown kevin_lee ??

I'd need to refer to the list of all available filesystems on my system and find this user joe_black and do a chown.

Could you please help me out?

Thanks.
Trond Haugen
Honored Contributor

Re: finding filesystems /dirs/files belonging to a user

As Michael pointed out xargs is faster. But also if you use a list of filesystems you would want to use the '-xdev' options to find to avoid searching filesystems more than once.

Regards,
Trond
Regards,
Trond Haugen
LinkedIn
Ralph Grothe
Honored Contributor

Re: finding filesystems /dirs/files belonging to a user

Hi Chern,

yes that's what I meant, forget about the loop.
But if you are interested in all locally mounted filesystems of your system, I think you do not need to hold an extra list in a file.
I would rather parse the output of mount or better bdf

e.g.

find $(bdf -l|awk '{print $NF}') -xdev -user joe_black | xargs chown new_owner

Beware to use the -xdev flag of find (read the find manpage) to prevent subdecending into other submounts.
Madness, thy name is system administration
Ralph Grothe
Honored Contributor

Re: finding filesystems /dirs/files belonging to a user

Oops,

a little flaw, I forgot about the heading, which we have to get rid of.
Just insert a wee sed like this (there will probably be no mount on 'on' ;-)

find $(bdf -l|sed '1d'|awk '{print $NF}') ...
Madness, thy name is system administration
Ralph Grothe
Honored Contributor

Re: finding filesystems /dirs/files belonging to a user

Nonsense, we don't need the extra sed, because awk can do this on its own:

find $(bdf -l|awk 'NR>1 {print $NF}')
Madness, thy name is system administration
Ralph Grothe
Honored Contributor

Re: finding filesystems /dirs/files belonging to a user

Sorry,

I again forgot to think before I posted (thus making my last three posts a complete embarrasement of the senses)
Forget the bdf stuff altogether because this is the same as "find / ..."
%-P
Madness, thy name is system administration
Chern Jian Leaw
Regular Advisor

Re: finding filesystems /dirs/files belonging to a user

Ralph,

What does the %-P option do? I'm not able to find this option in the manpages for find.

Do I just say:
find ... %-P |xargs chown kevin_lee ??

Thanks.
Ralph Grothe
Honored Contributor

Re: finding filesystems /dirs/files belonging to a user

Oh sorry for misleading you,
this is no option at all but a silly kind of smily.
Just turn your head to the left and look at it.
The characters should resemble a confused mind (i.e. me) that sticks out his tongue.

:-)
Madness, thy name is system administration