Removing old user directories that are no longer Users in /etc/passwd


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing old user directories that are no longer Users in /etc/passwd
# 1  
Old 12-07-2011
Removing old user directories that are no longer Users in /etc/passwd

I am new to shell scripting, and have not done much programming in several years. So I am very rusty at this at best. I know my way around the linux command line, but actually scripting is something I have not done too much of.

I have been tasked to come up with a script that will pull all users from /etc/passwd. Then I need to find all the user directories in /home. If there is a user directory in /home that does not have a corresponding I need to delete that directory and everything in it.

I know I can use
Code:
awk -F: '{ if ($3 > 999) { print $1 }}' /etc/passwd

To find all the users above userid 999.

I know
Code:
ls /home/

will pull up all the different user directories.

My thinking of how the script should logically run is something as follows.

Find all users in /etc/passwd above userid 999
find all users in /home
compare users in /home to users in /etc/passwd
remove all user directories in /home who are not in /etc/passwd

So any help with this would be greatly appreciated.
# 2  
Old 12-07-2011
I'm worried about this 'greater than 999' requirement. It sounds backwards. Would it be okay to delete /home/root since root's uid is less than 999? Probably not.

I suspect you're supposed to consider the ownerships of the home directories themselves, right? Their UID's will be preserved even when the user's deleted.

What's your system? What's your shell?
# 3  
Old 12-07-2011
Shell is bash. Systems are Redhat, Centos, and Ubuntu for testing.

Basically all the users would have had an userid starting at either 500 on the Redhat systems or 1000 on the Ubunutu systems. I was using the > 1000 to get rid of all the user accounts that are system accounts and such in /etc/passwd.

If there was a directory /home/root and there was not a user with a userid of root > 1000 then yes I would want to delete it.
# 4  
Old 12-07-2011
Even if that means it belongs to root, uid 0, and removing that dir would prevent the administrator from logging in ever again? Some FTP daemons have a /home/ftp, and a UID less than 1000, that'd vanish too with unknown results.

I think you need to rethink your criteria.
# 5  
Old 12-07-2011
I am starting to see your point. So I guess I should change it to I would like to remove all user directories in /home/ that have a uid > 1000 and are not also in /etc/passwd

So logically my code would go something like
Code:
if uid of folder in /home/ > 1000 and not a uid in /etc/passwd
    delete folder
Then loop for every folder in /home/

# 6  
Old 12-07-2011
Looking in find's options, I see this useful thing:

Code:
 %u     File's user name, or numeric user ID if the user has no name.

So find itself can tell you when a user's been deleted! Lovely.

So you can do this:
Code:
find /home -type d -mindepth 1 -maxdepth 1 -printf '%u %f\n' |
        # Print only pure numbers > 1000
        awk '$1 ~ /^[0-9]*$/ && $1 > 1000' |
        while read UID DIR
        do
                echo "/home/${DIR} has uid ${UID} and no username:"
                ls -ld "/home/${DIR}"
        done

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Solaris

Giving read write permission to user for specific directories and sub directories.

I have searched this quite a long time but couldn't find the right method for me to use. I need to assign read write permission to the user for specific directories and it's sub directories and files. I do not want to use ACL. This is for Solaris. Please help. (1 Reply)
Discussion started by: blinkingdan
1 Replies

2. Solaris

Can't change users passwd

Have an issue with a user or root changing the user's passwd. We run the passwd command and a complex passwd is entered a message is displayed, "passwd is based on a dictionary word." We do have a dictionary file and I know for a fact the complex passwd is not in the list. This happens on a... (3 Replies)
Discussion started by: solizkewl
3 Replies

3. Solaris

Impact on existing users when changing passwd defaults

Hi Administering Solaris Systems - Solaris 10 mostly. If I change the /etc/default/passwd settings - E.G. to increase minimum passwd length, then what happens to existing users with passwords shorter than this. presumably they are not affected until next time they want to change password. ... (4 Replies)
Discussion started by: Mudshark
4 Replies

4. UNIX for Advanced & Expert Users

Determining if user is local-user in /etc/passwd or LDAP user

Besides doing some shell-script which loops through /etc/passwd, I was wondering if there was some command that would tell me, like an enhanced version of getent. The Operating system is Solaris 10 (recent-ish revision) using Sun DS for LDAP. (5 Replies)
Discussion started by: ckmehta
5 Replies

5. Shell Programming and Scripting

Number of users in passwd

This command prints out username/users in /etc/passwd: cut -d ':' -f '1,5' /etc/passwd | sort I wonder if I also, after above commands output, can get an output that lists number of users in the group? I need to use uniq to get rid of duplicates. I´ve tried this, but cant get it right, can... (5 Replies)
Discussion started by: oskis
5 Replies

6. UNIX for Dummies Questions & Answers

User Name and Password List/adding and removing users.

Hello everyone and let me start off by thanking anyone who can help with this. I work for a company that uses Unix as one of their servers. I'm not at all familar with Unix beyond logging after I restart the server:rolleyes: I'm looking for some command that will bring me up a list of current... (3 Replies)
Discussion started by: disgracedsaint
3 Replies

7. UNIX for Dummies Questions & Answers

editing sqlplus id@passwd in multiple scripts, users and directories

hi all, i was given by my supervisor a task to search for scripts which contain oracle sqlplus i.e "myusername/mypasswd @myDB" in every /home/userfolder, which are, all the scripts made by different user. I've done some find command to search string for sqlplus, but it may up too long to respond.... (8 Replies)
Discussion started by: Helmi
8 Replies

8. UNIX for Advanced & Expert Users

Users logged in longer than machine uptime?

SCO 5.06 Anyone ever have an issue where: uptime returns: SCO:/# uptime 4:40pm up 4:50, 1 user, load average: 0.00, 0.00, 0.00 w returns: SCO:/# w User Tty Login@ Idle JCPU PCPU What root tty01 - 72:20 - - -ksh I've rebooted yet... (1 Reply)
Discussion started by: gseyforth
1 Replies

9. Shell Programming and Scripting

append 3 users in /etc/passwd

I am looking to add 3 lines in /etc/passwd via a script. Can you please give me an idea on how to write a script that can do that? (3 Replies)
Discussion started by: melanie_pfefer
3 Replies
Login or Register to Ask a Question