- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: How to write a script to find the home directo...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-05-2011 06:07 AM
01-05-2011 06:07 AM
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.
Solved! Go to Solution.
- Tags:
- passwd
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-05-2011 06:26 AM
01-05-2011 06:26 AM
SolutionCreate 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-05-2011 06:38 AM
01-05-2011 06:38 AM
Re: How to write a script to find the home directories of user IDs that are not available in /etc/passwd
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-05-2011 06:47 AM
01-05-2011 06:47 AM
Re: How to write a script to find the home directories of user IDs that are not available in /etc/passwd
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-05-2011 06:57 AM
01-05-2011 06:57 AM
Re: How to write a script to find the home directories of user IDs that are not available in /etc/passwd
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-05-2011 07:18 AM
01-05-2011 07:18 AM
Re: How to write a script to find the home directories of user IDs that are not available in /etc/passwd
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-05-2011 07:32 AM
01-05-2011 07:32 AM
Re: How to write a script to find the home directories of user IDs that are not available in /etc/passwd
I am not very good at Unix scripts...
So could you please give the exact script...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-05-2011 08:41 AM
01-05-2011 08:41 AM
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...
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-05-2011 08:43 AM
01-05-2011 08:43 AM
Re: How to write a script to find the home directories of user IDs that are not available in /etc/passwd
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-05-2011 09:31 AM
01-05-2011 09:31 AM
Re: How to write a script to find the home directories of user IDs that are not available in /etc/passwd
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2011 06:16 AM
01-06-2011 06:16 AM
Re: How to write a script to find the home directories of user IDs that are not available in /etc/passwd
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2011 06:18 AM
01-06-2011 06:18 AM