LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   create multiple directories in bash? (https://www.linuxquestions.org/questions/programming-9/create-multiple-directories-in-bash-618390/)

Count Zero 02-03-2008 11:46 AM

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!

unSpawn 02-03-2008 11:59 AM

Quote:

Originally Posted by Count Zero (Post 3044699)
experimenting without any success at all.

Show us what you've tried?

pixellany 02-03-2008 12:03 PM

I would assume that you would use a loop. Here is just one example:
Quote:

for (( i=1; i<10; i++ )); do mkdir dir$i; done
(makes 9 new directories: dir1, dir2,......dir9

Count Zero 02-03-2008 12:13 PM

Yes!
 
Quote:

Originally Posted by pixellany (Post 3044712)
I would assume that you would use a loop. Here is just one example:
(makes 9 new directories: dir1, dir2,......dir9

Yes! That worked perfectly. Thanks!

colucix 02-03-2008 12:32 PM

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)

Count Zero 02-03-2008 03:04 PM

Quote:

Originally Posted by colucix (Post 3044734)
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)

I was trying to use the wildcard variant of range, I believe that is expressed as [1-9], right? Anyway, I tried your option and I got directories like so: foo001 002 003 and so on, i.e. only the first was named and the rest was given a numeric value only. Any ideas why? Still, seq was a nifty command, never heard of it before. Thanks.

pixellany 02-03-2008 03:58 PM

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.

colucix 02-03-2008 04:16 PM

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)
but at this point it is more simple in the pixellany's way, or this little variant to get leading zeros:
Code:

for (( i=1001; i<=1150; i++ ))
do
  mkdir foo${i:1}
done


makyo 02-03-2008 04:33 PM

Hi.

Similarly, this:
Code:

echo $( seq --format="xx%03.f" 9 12 )
produces:
Code:

xx009 xx010 xx011 xx012
See info coreutils seq for details ... cheers, makyo

tonyfreeman 02-03-2008 06:26 PM

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!

jschiwal 02-03-2008 06:37 PM

If you didn't use leading zero's you could use "mkdir foo{1..1000}".

Count Zero 02-05-2008 10:33 AM

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!

Count Zero 07-06-2008 02:59 AM

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.

colucix 07-06-2008 03:34 AM

Quote:

Originally Posted by Count Zero (Post 3205363)
$BASENAME=Stuff
$STARTDIR=1
$ENDDIR=102

Maybe the problem is here. The assignments you've used for testing are wrong: you have not to put the leading $ sign to assign a value to a variable, that is
Code:

BASENAME=Stuff
STARTDIR=1
ENDDIR=102

is the correct form. The following works for me:
Code:

mkdir $BASENAME$(seq -w -s " $BASENAME" $STARTDIR $ENDDIR)
As you already pointed out, this statement - among the others suggested - works for any number of directories, whereas to use the other forms you must know the number of digits a priori.

Count Zero 07-06-2008 03:46 AM

Quote:

Originally Posted by colucix (Post 3205372)
The following works for me:
Code:

mkdir $BASENAME$(seq -w -s " $BASENAME" $STARTDIR $ENDDIR)

Hi,

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.