Solved

Fixing user files

Posted on 2009-07-13
5
231 Views
Last Modified: 2012-05-07
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.
0
Comment
Question by:Legatie
5 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 24843122
Do you want all files to belong to their home? of just  /home/abcd files? or all files with an owner that does not exist in /etc/passwd
Do you want to do anything with files not under /home?
0
 
LVL 4

Expert Comment

by:djmounce553
ID: 24843211
You can do this using pipes in the following manner.
find / \( -nouser -o -nogroup \) | xargs chown USERNAME
Just specify the user to change it to and you're all set!
0
 
LVL 48

Expert Comment

by:Tintin
ID: 24843566
Once you've determined which invalid UiD's belong to which user, you can do

find /home -owner 12345 | xargs chown abcd
0
 
LVL 40

Accepted Solution

by:
omarfarid earned 500 total points
ID: 24847361
try this script

awk -F":" '{ print $3 " " $4 " " $6 }' /etc/passwd | while read uid gid home
do
   find $home -nouser -o -nogroup | xargs chown $uid:$gid
done
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 24852217
why B grade? What is missing in the solution / script?
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question