Operating System - HP-UX
1827328 Members
5872 Online
109962 Solutions
New Discussion

Re: How to write a script to find the home directories of user IDs that are not available in /etc/passwd

 
SOLVED
Go to solution
senthil_kumar_1
Super Advisor

How to write a script to find the home directories of user IDs that are not available in /etc/passwd

Hi All,

We are maintaining all user's home directories under /home as usual...

But there are lot of directories availble but those are not belongs to any user id (that is those user IDs must be deleted already)...

Now I would like to write a script to find out all those directories.



11 REPLIES 11
James R. Ferguson
Acclaimed Contributor
Solution

Re: How to write a script to find the home directories of user IDs that are not available in /etc/passwd

Hi:

Create a list (or file) of all of the user names found in the '/home' directory. Using 'ls -1' (that's a number one) would suffice.

Now, read the list (file) of user names and query '/etc/passwd' for an exact match. If you find a match, skip on. If you don't find a match, print the user name since it may represent an orphaned directory as defined by your criteria.

I would suggest more rigorous maintenance if/when user IDs are deleted in the future.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: How to write a script to find the home directories of user IDs that are not available in /etc/passwd

Hi (again):

A shell script as simple as this should accommodate you.

# cat ./expose
#!/usr/bin/sh
ls -1 /home | while read USER X
do
grep -q "^${USER}:" && continue || echo "${USER}"
done
exit 0

Regards!

...JRF...
Doug O'Leary
Honored Contributor

Re: How to write a script to find the home directories of user IDs that are not available in /etc/passwd

Hey;

A minor modification: The script above assumes that people's home directories are named after their account. Generally a valid assumption, but, not necessarily the case.

Following the same general logic:

for d in /home/*
do
grep -q ":${d}:" /etc/passwd && continue && echo ${d}
done
/home/lost+found

My $.02.. I like the grep -q syntax. I'll have to remember that.

I've generally done the

grep ... > /dev/null 2>&1
[[ $? -eq 0 ]] && ... || ...

Same result, but yours is cleaner.

Doug

------
Senior UNIX Admin
O'Leary Computers Inc
linkedin: http://www.linkedin.com/dkoleary
Resume: http://www.olearycomputers.com/resume.html
James R. Ferguson
Acclaimed Contributor

Re: How to write a script to find the home directories of user IDs that are not available in /etc/passwd

Hi (again):

Oops, the line:

grep -q "^${USER}:" && continue || echo "${USER}"

...should of course be:

grep -q "^${USER}:" /etc/passwd && continue || echo "${USER}"

Doug's point that a common convention is to name home directories with the login (account) name is only a convention is quite correct. My script assumes that to be true.

Regards!

...JRF...
Mel Burslan
Honored Contributor

Re: How to write a script to find the home directories of user IDs that are not available in /etc/passwd

my version would be:

USERLIST=$(cat /etc/passwd|cut -d: -f1)
cd /home
for HOMEDIR in $(ls -1);do
echo $USERLIST | grep $HOMEDIR; result=$?
if [ $result -ne 0 ]
then
echo $HOMEDIR" is not associated to an existing user"
fi


one caveat: if the number of users on this system is unreasonably large, like in the order of hundreds, the USERLIST variable may not be dependable and the whole construct becomes unreliable.
________________________________
UNIX because I majored in cryptology...
senthil_kumar_1
Super Advisor

Re: How to write a script to find the home directories of user IDs that are not available in /etc/passwd

Hi James,

I am not very good at Unix scripts...

So could you please give the exact script...
James R. Ferguson
Acclaimed Contributor

Re: How to write a script to find the home directories of user IDs that are not available in /etc/passwd

Hi Senthil:

> Hi James, I am not very good at Unix scripts...So could you please give the exact script...

I did, but here it is again with the corrected line:

# cat ./expose
#!/usr/bin/sh
ls -1 /home | while read USER X
do
grep -q "^${USER}:" /etc/passwd && continue || echo "${USER}"
done
exit 0

That stated, I like *Doug's better* since his makes no assumptions about naming conventions but simply queries '/etc/passwd' for directory names in '/home' and reports those that do not exist.

Regards!

...JRF...
Doug O'Leary
Honored Contributor

Re: How to write a script to find the home directories of user IDs that are not available in /etc/passwd

Hey;

Correction:


or d in /home/*
do
grep -q ":${d}:" /etc/passwd && continue || echo ${d}
done
/home/lost+found

I had to '&&' instead of '&& and ||'... bad thing about typing this in from scratch...

Doug

------
Senior UNIX Admin
O'Leary Computers Inc
linkedin: http://www.linkedin.com/dkoleary
Resume: http://www.olearycomputers.com/resume.html
Dennis Handly
Acclaimed Contributor

Re: How to write a script to find the home directories of user IDs that are not available in /etc/passwd

>Mel: one caveat: if the number of users on this system is unreasonably large, like in the order of hundreds

It would more likely to have to be 100s of thousands.

You should also use "grep -q" to prevent output for all of the correct directories.
Don Mallory
Trusted Contributor

Re: How to write a script to find the home directories of user IDs that are not available in /etc/passwd

A simple find will do it for you.

The for line is really only to break out only locally mounted areas under vg00. You could easily replace it with a known list, text file, etc. (try running bdf | grep vg00 | awk '{ print $6 }' | grep -v "^/$" on it's own to see what your list should look like.)

You will end up with a list of dirs that have either no group, or no user (noting the two find commands below). Output will be a text file.


for DIR in `bdf | grep vg00 | awk '{ print $6 }' | grep -v "^/$"`
do
find $DIR -nouser -type d -exec ls -l {} \; >> /tmp/nouser.$$.txt 2>&1
find $DIR -nouser -exec chown root {} \;
find $DIR -nogroup -type d -exec ls -l {} \; >> /tmp/nogroup.$$.txt 2>&1
find $DIR -nogroup -exec chgrp sys {} \;
done


Best regards,
Don
Don Mallory
Trusted Contributor

Re: How to write a script to find the home directories of user IDs that are not available in /etc/passwd

Oh, sorry, it will also chown the dirs to root:sys so that the dirs are locked. If you don't want this part, remove the chown/chgrp lines.