create multiple directories in bash? [SOLVED]
Hi,
I've been trying to create multiple directories using bash. The directories I need to create will look something like foo001 foo002 foo003 ... foo150 or something. Now, I now very little about bash scripting but my first take was that this should be possible with like a single command in the terminal, using makedir and a set for the desired range, e.g. [001-150]. I've been reading on mkdir and bash man pages and googling around and experimenting without any success at all. Does anyone have a solution to this? I'd appreciate any and all help I can get. Thanks! |
Quote:
|
I would assume that you would use a loop. Here is just one example:
Quote:
|
Yes!
Quote:
|
FYI a range in bash is expressed as {m..n} where m and n are integers. Another way to get a sequence of numbers is the command seq. In your example a one-line command would have been
Code:
mkdir foo$(seq -w 1 150) |
Quote:
|
The only way I learn these things is to break them down:
echo `seq 1 4` produces 1 2 3 4 thus: echo foo`seq 1 4` produces foo1 2 3 4 PS: `command` is the same as $(command)----those are "backtics", not single quotes. [1-9] is used in regexes to match any digit--it's not for producing ranges of numbers. |
Uh, sorry... my mistake! The command substitution expands leaving the string foo only at the beginning. We could use a little trick like this
Code:
mkdir foo$(seq -w -s " foo" 1 150) Code:
for (( i=1001; i<=1150; i++ )) |
Hi.
Similarly, this: Code:
echo $( seq --format="xx%03.f" 9 12 ) Code:
xx009 xx010 xx011 xx012 |
Great Tips on 'seq' command!
These are GREAT tips using the 'seq' command. I've never heard of it. This will be a huge bonus to have my my mental bash toolkit! Thanks!
|
If you didn't use leading zero's you could use "mkdir foo{1..1000}".
|
I just want to thank everyone for their quick and helpful replies. I learned quite a lot, including some very basic stuff such as the difference between range and regex. I'm new to bash and I've been struggling with the subject through a book and some guides from the web but I just couldn't find any way to approach this and google didn't help. I'll be learning a lot more by dissecting all the examples you provided me with. Thanks!
|
Hi again,
I thought I'd revive this thread by expanding the question. The earlier examples helped me out nicely but I've been fiddling around with a short script to do it for me. The script uses three variables, one for the base of the directory name, one for the starting directory number and one for the ending directory number. It takes its input from the command 'read' and simply prompts the user for the answer, so if the variables would be set (by input from read) like this: $BASENAME=Stuff $STARTDIR=1 $ENDDIR=102 it would create 102 directories named like this: Stuff001, Stuff002, Stuff003 [...] Stuff101, Stuff102 In order to get the numbering I used the command seq as shown earlier in the thread. It seem like a good solution as the -w option very easy gives the right amount of leading zeros depending on the number of directories. Now, the problem I've run into is to get mkdir to create a name that consists of a variable ($BASENAME) and a command. (`seq -w $STARTDIR $ENDDIR`). I tried some variant of colucix example (post #8) but as I got the basename set by a variable I just can't seem to get it right. Does anyone have any idea on how to solve this? Thanks. |
Quote:
Code:
BASENAME=Stuff Code:
mkdir $BASENAME$(seq -w -s " $BASENAME" $STARTDIR $ENDDIR) |
Quote:
Thanks for the reply. I tried that but it doesn't work for me. It creates the correct number of directories but they only have the numeric name i.e. (001, 002, 003 [...] 101, 102. So, when I set BASENAME=test, STARTDIR=1, ENDDIR=12 and run that command I get 12 directories named 01, 02, 03 [...] 11, 12 instead on test01, test02 etc but I don't know why. Cheers |
All times are GMT -5. The time now is 07:30 PM. |