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
  • 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
    Commented Sep 23, 2016 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
    Commented Sep 23, 2016 at 11:58
  • 2
    Not sure why do you perceive the question so bad, it rather matters of a simple script of 1 LOC
    – Arefe
    Commented Apr 13, 2018 at 9:40

1 Answer 1

9

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.

1
  • 1
    Very helpful answer
    – Arefe
    Commented Apr 13, 2018 at 9:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .