I have a list that looks like this in a file called
JOHN DOE JANE DOE ADAM SMITH SARAH BROWN SUSIE JOHNSON
Is there a script that I can run in the Terminal that will create folders from each line in this list?
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.Sign up to join this community
There's an easy way to run a command for each line of a text file, and it doesn't require a script which would be overkill for a single command like
mkdir. Use the
xargs command like this:
xargs -tI % mkdir % < names.txt
-I option tells
xargs to run a command for each line from
STDIN. In this case, STDIN comes from reading the
names.txt file with
< names.txt. The
% character is a replacement string that
xargs uses to as a placeholder for a line from the file. This means that everywhere
% in the command,
% is replaced by a line from the file.
-t option causes
xargs to print each command before it's executed. It's not necessary, but it can be helpful for more complicated problems.
xargs runs with the sample file, the output looks like this:
mkdir JOHN DOE mkdir JANE DOE mkdir ADAM SMITH mkdir SARAH BROWN mkdir SUSIE JOHNSON
mkdir commands make a new folder with the names from the