1

I have been trying to write bash code which deletes all users listed in a txt file in Ubuntu, however I am not sure how to do this.

For example say that the text in a file has two users, A and B. What I am trying to figure out is how to actually delete the users A and B from the system.

This is my pseudocode:

numusersdel = wc -l userstobedel.txt 
for ((i=0;i < $numusersdel; i++)); do
    usertobedel = grab the username of the user from number line i
    deluser $usertodel
done

Please suggest how I can solve this. Thanks.

Vince G
  • 25
  • 5

1 Answers1

1

You can use a while loop and read the file userstobedel.txt line by line:

while IFS= read -r usertobedel
do
    deluser "$usertobedel"
done < userstobedel.txt

IFS= and -r are explained here.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108