Deleting User Accounts With userdel in Linux
The userdel command takes a single argument, which is the login name of the account to delete. If you supply the optional -r option, it also deletes the user’s home directory and all the files in it. To delete the user account with login name mary, you would type this:
# userdel mary
To wipe out her home directory along with her account, type this:
# userdel -r mary
Files owned by the deletec user but not located in the user’s home directory will not be deleted. The system administrator must search for and delete those files manually. The find command comes in very handy for this type of task.
Using find to locate and change user files:
– find / -user mary : Search the entire file hierarchy (start at /) for all files and directories owned by mary and print the filenames to the screen.
– find /home -user mary -exec rm -i {} \; : Search for all files and subdirectories under /home owned by mary. Run the rm command interactively to delete each file.
– find / -user mary -exec chown jenny {} \; : Search for all files and subdirectories under /home that are owned by user mary and run the chown command to change each file so that it is owned by jenny instead.
– find / -uid 500 -exec chown jenny {} \; : This command is basically the same as the previous example, but it uses the user ID number instead of the user name to identify the matching files. This is useful if you have deleted a user before converting her files.
There are a few common things about each invocation of the find command. The first parameter is always the directory to start the recursive search in. After that come the file attributes to match. You can use the -exec parameter to run a command against each matching file or directory. The {} characters designate where the matching filename should be filled in when find runs the -exec option. The \; at the end simply tells Linux where the command ends. These are only a few of find’s capabilities.
For more information on using find, check the man page. Type man find to view the page.
