0

I need to make 5 directories (name1, name2, name3, name4, name5) by command line using mkdir and seq.

If I use:

mkdir name{`seq -s , 1 5`}

It results in one directory: name{1,2,3,4,5}

What command should I use instead?

  • In Bash you can say for d in $(seq 5); do mkdir name$d; done. Read the manual page for your shell; brace expansion happens before command substitution. P.S. Are you sure that your shell does brace expansion? – AlexP Oct 15 '17 at 23:13
  • If any of the answers solved your problem, please indicate it by clicking the checkmark next to it; thank you! – Jeff Schaller Oct 22 '17 at 11:49
3
seq 1 42 | mkdir name{1..5}

Or, if you don't need the answer to Life, the Universe, and Everything, drop the seq and let bash's brace expansion the the work by itself:

mkdir name{1..5}

But, if you really need to use seq because this is homework:

mkdir $(seq -f "name%d" 5)
  • 5
    In case any readers are confused: seq 1 42 | mkdir name{1..5} accomplishes the same thing as mkdir name{1..5} because the mkdir command does not read from stdin, so the sequence of numbers seq produces is simply ignored. It's a clever way of technically using the seq command, to comply with contrived requirements. – Eliah Kagan Oct 16 '17 at 3:28
2

If for some reason you don't just want to use brace expansion (also see below), you might consider:

seq 5 | xargs -I N mkdir nameN

That creates name1, name2, name3, name4, and name5. It works even if your shell does not support brace expansion (but bash does). The xargs command builds and runs commands. That specific xargs command runs commands like mkdir nameN, but with N replaced with each word of the input (which is piped in from seq). Run man xargs or read this page for more information on the -I option.

If this is for a homework assignment, perhaps you are not allowed to use a third command (xargs). But in "real life," you're less likely to have such a restriction. Furthermore, if you're doing this for learning purposes, it's an opportunity to learn the xargs command--if you don't already know it--which is useful in a variety of situations.

You can also use a shell loop, as AlexP suggested (for d in $(seq 5); do mkdir name$d; done). That for loop uses the $( ) syntax to perform command substitution in order to obtain the list 1 2 3 4 5 from seq, then uses parameter expansion ($d) to form the directory name in each mkdir command.

You should be aware that all those methods rely on the absence of whitespace in the directory names, although it is possible to modify them so that they do not.

But you're using bash and, like many shells, it supports brace expansion. Now, maybe you need your script to be portable to shells that don't (such as dash). If that's what you need, you should not use brace expansion at all. Otherwise, you should follow Jeff Schaller's recommendation of simply writing mkdir name{1..5}.

  • According to a quick search here, several other shells support brace expansion: unix.stackexchange.com/a/269713/117549 – Jeff Schaller Oct 16 '17 at 9:21
  • @JeffSchaller Yes, many shells support brace expansion (but not all). Do you think my answer makes it sound like bash is the only one? If so, I'd be pleased to reword it; if not, I'll probably avoid making it any more complicated than it already is. – Eliah Kagan Oct 16 '17 at 9:23
  • Maybe it's the way I'm reading the very last sentence. It seems less likely that they'll run into a shell that doesn't support it. – Jeff Schaller Oct 16 '17 at 9:26
  • @JeffSchaller You're right, it was potentially confusing. I've reworked that paragraph to make it clearer. As for how likely one is to run into a shell that doesn't support brace expansion, it's actually quite common these days on Debian and its derivatives, since for some years /bin/sh has been dash rather than bash. On the other hand, my guess is that being on a system that has no Bourne-style shell installed and usable that supports brace expansion is probably pretty rare today, aside from very minimal BusyBox-based systems. – Eliah Kagan Oct 16 '17 at 9:36

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.