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?
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?
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)
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}.