1751805 Members
5223 Online
108781 Solutions
New Discussion юеВ

Re: Fixing user files

 
Robert Legatie
Advisor

Fixing user files

Hello,

I am trying to fix files which do not have users. I am using the following find command for it.
find / \( -nouser -o -nogroup \) | xargs ls -ld
Suppose it gives us an output of
-rwxr----- 1 12345 user_unix 943 Jan 13 12:12 /home/abcd/.profile

From the above output it is obvious that this file belongs to user "abcd" but is owned by 12345 which does not exist in /etc/passwd.

Can someone help me with a command or maybe a couple of lines of script to change the file to be owned by user "abcd" from "12345". I have many files like this that needs to be changed.

Thanks in advance.

11 REPLIES 11
Mel Burslan
Honored Contributor

Re: Fixing user files

is there any particular reason why you can not use this:

find / \( -nouser -o -nogroup \) | xargs chown abcd

________________________________
UNIX because I majored in cryptology...
Pete Randall
Outstanding Contributor

Re: Fixing user files

find / \( -user 12345 \) |xargs chown abcd

would do it. If you have many of them you could redirect the output of your "nouser/nogroup/ command to a file and then use that as input to a for loop.


Pete

Pete
Mel Burslan
Honored Contributor

Re: Fixing user files

Otherwise, you can send the output to a file

find / \( -nouser -o -nogroup \) | xargs ls -ld > /tmp/ownerless_files

then process this file in a way like this:


cat /tmp/ownerless_files | while read line
do
owner=`echo $line | awk {'print $3'}`
group=`echo $line | awk {'print $4'}`
filename=`echo $line | awk {'print $9'}`

if [ "${owner}" = "12345" ]
then
chown abcd ${filename}
fi

#replicate the above 'if' block as many times as necessary depending on
#conditions you want to test for users and groups

done

This should do it.
________________________________
UNIX because I majored in cryptology...
James R. Ferguson
Acclaimed Contributor

Re: Fixing user files

Hi:

# find /path \( -nouser -o -nogroup \) -exec chown abcd {} +

Be careful. I would limit your '/path' to that which is known to require change. You don't want to capricioulsy transfer ownership of files.

Regards!

...JRF...
Robert Legatie
Advisor

Re: Fixing user files

I am getting a syntax error "do" is not matched. I am using the following:
#!/usr/bin/sh
find / \( -nouser -o -nogroup \) | xargs ls -ld > /tmp/test
cat /tmp/test | while read line
do
owner=`echo $line | awk {'print $3'}`
group=`echo $line | awk {'print $4'}`
filename=`echo $line | awk {'print $9'}`
Steven Schweda
Honored Contributor

Re: Fixing user files

Exactly what you want is not entirely clear.

If you want all the files under "/home/abcd/"
to be owned by user "abcd", then you might
write a script which loops through all the
directories "/home/*", and does a "find -exec"
on each one. If you want only the owner-less
or group-less files to be affected, then
that's also possible.

While working on your script, you might wish
to add an "echo" command to your "-exec"
clause, so that you can see what will happen
_before_ you wreck something.
James R. Ferguson
Acclaimed Contributor

Re: Fixing user files

Hi:

> I am getting a syntax error "do" is not matched.

Well, try adding a closing 'done'. You are making this harder than it needs to be.

# find /path \( -nouser -o -nogroup \)|xargs -t -n1 chown abcd

...gives you a trace and the change.

Regards!

...JRF...
Robert Legatie
Advisor

Re: Fixing user files

Steven,
Right, I only want to change those found to be unowned by the find command. Can you please spell out the loop here on how do i change it.

thanks.
Mel Burslan
Honored Contributor

Re: Fixing user files

find / \( -nouser -o -nogroup \) | xargs ls -ld > /tmp/test
cat /tmp/test | while read line
do
owner=`echo $line | awk {'print $3'}`
group=`echo $line | awk {'print $4'}`
filename=`echo $line | awk {'print $9'}`

# do something here

done

this above is your construct.

you can replace

# do something here

line with either:

(1)
*********************************
if [ "${owner}" = "12345" ]
then
chown abcd ${filename}
fi
*********************************

or

(2)
*********************************
chown abcd ${filename}
*********************************

Option number (2) is the same as what any of the find blah-blah | xargs chown blah-blah will give you as the result.

Your problem definition is very vague. If you state what you want done from the get-go, this thread would not stretch this long. Sysadmins are pretty much like computers. They do what you "tell" them to do not what "want" done. WIsh I have mind reading capabilities but it is coming in my Mel Burslan V2.0 in the next life.
________________________________
UNIX because I majored in cryptology...