Crimius Crimius - 20 days ago 9
Linux Question

Can I pipe a list of usernames to useradd?

I have a collection of folders that I'm filtering down and need to create user accounts to go with them. Can I pipe the output from my

grep
to
useradd
as usernames?

Context: FTP migration. Moving to a Linux FTP server from Windows, but the storage is remaining where it is. The folders are old FTP user folders, and the folder names match the FTP login names. Looking at:

ls /media/OldFTP | grep -Ev '^[[:digit:]]{4}|^P[[:digit:]]{4}|^W[[:digit:]]{4}' | useradd -M -g ftpusers -N -p [encrytedPassword]


I'm trying to pass the output of my grep search to the username field for useradd.

Answer

If the encryptedPassword will be the same for every user, you can use xargs -n1 <cmd> to run a command once for each argument passed on stdin.

ls /media/OldFTP |
    grep -Ev '^[[:digit:]]{4}|^P[[:digit:]]{4}|^W[[:digit:]]{4}' |
    xargs -n1 -d'\n' useradd -M -g ftpusers -N -p [encryptedPassword]