1

I have a list that looks like this in a file called names.txt:

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?

  • 3
    Sorry, but this is not a script-writing service. We can help with a specific issue where you're stuck. Please tell us what you have done so far & what the results were. – Tetsujin Sep 23 '16 at 10:55
  • 1
    Please note that superuser.com is not a free script/code writing service. If you tell us what you have tried so far (include the scripts/code you are already using) and where you are stuck then we can try to help with specific problems. You should also read How do I ask a good question?. – DavidPostill Sep 23 '16 at 11:58
  • 2
    Not sure why do you perceive the question so bad, it rather matters of a simple script of 1 LOC – Chaklader Asfak Arefe Apr 13 '18 at 9:40
7

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

The -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 xargs sees % in the command, % is replaced by a line from the file.

The -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.

When 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

and the mkdir commands make a new folder with the names from the names.txt file.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.