Massive account removal


There is a need sometimes to remove more than one user account at once on some linux server. In my case, such situation occured on ftp server, where user accounts have been created with expiration date. The accounts are blocked automatically when expired. Information about blocked accounts we can get by:

passwd -Sa | grep " L "

Based on above we can search for the accounts for removal. In my case these were all accounts, where home directory name contained Vendors, and the parent directory name had 3 digit at the end. I could find all the accounts for removal this way::

grep "3/Vendors" /etc/passwd

To reduce above command result to the account list:

grep "3/Vendors" /etc/passwd | awk -F ":" '{print $1}'

And finally to redirect userdel command:

grep "3/Vendors" /etc/passwd | awk -F ":" '{print $1}' | xargs -n1 userdel -r