Batch Add and Delete Linux Users with UID ≥ 1000 Using awk and xargs
This guide shows how to batch‑create and batch‑remove Linux users whose UID is 1000 or higher by extracting usernames from /etc/passwd with awk and feeding them to useradd or userdel via xargs, including command examples and detailed explanations of each step.
Requirement Analysis
In Linux, system accounts (e.g., root, bin, daemon) usually have UID less than 1000. Regular user accounts start at UID 1000, although this can be changed via /etc/login.defs or administrator settings. To manage non‑system users, we can extract usernames with UID ≥ 1000 from /etc/passwd using awk and pipe the result to xargs for batch operations.
Practical Steps
2.1 Batch add users
echo -e "u1
u2
u3
u4" | xargs -L 1 useraddAfter running the command, you can verify the new entries with: tail /etc/passwd The /etc/passwd file uses colon ( :) as a field separator. The first field is the username, and the third field is the UID.
2.2 Extract usernames with UID ≥ 1000 using awk
awk -F ':' '$3>=1000{print $1}' /etc/passwdThis prints all usernames whose UID is 1000 or greater.
2.3 Batch delete users
awk -F ':' '$3>=1000{print $1}' /etc/passwd | xargs -L 1 userdel -rThe command removes the identified users and their home directories ( -r flag).
awk -F ':' '$3>=1000{print $1}' /etc/passwd parses /etc/passwd with : as the delimiter and prints the first field (username) when the third field (UID) is ≥ 1000.
The pipe symbol ( | ) feeds the list of usernames into the next command.
xargs -L 1 takes one line at a time from the previous output and passes it as an argument to userdel -r , ensuring correct handling of usernames that might contain spaces or special characters.
userdel -r deletes the user account and recursively removes the user’s home directory and mail spool.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
