LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-03-2008, 11:46 AM   #1
Count Zero
Member
 
Registered: Feb 2008
Distribution: Debian wheezy
Posts: 130

Rep: Reputation: 15
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!

Last edited by Count Zero; 02-03-2008 at 12:15 PM.
 
Old 02-03-2008, 11:59 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by Count Zero View Post
experimenting without any success at all.
Show us what you've tried?
 
Old 02-03-2008, 12:03 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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
 
Old 02-03-2008, 12:13 PM   #4
Count Zero
Member
 
Registered: Feb 2008
Distribution: Debian wheezy
Posts: 130

Original Poster
Rep: Reputation: 15
Yes!

Quote:
Originally Posted by pixellany View Post
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!
 
Old 02-03-2008, 12:32 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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)
 
Old 02-03-2008, 03:04 PM   #6
Count Zero
Member
 
Registered: Feb 2008
Distribution: Debian wheezy
Posts: 130

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by colucix View Post
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.
 
Old 02-03-2008, 03:58 PM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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.

Last edited by pixellany; 02-03-2008 at 04:00 PM.
 
Old 02-03-2008, 04:16 PM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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
 
Old 02-03-2008, 04:33 PM   #9
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
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
 
Old 02-03-2008, 06:26 PM   #10
tonyfreeman
Member
 
Registered: Sep 2003
Location: Fort worth, TX
Distribution: Debian testing 64bit at home, EL5 32/64bit at work.
Posts: 196

Rep: Reputation: 30
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!
 
Old 02-03-2008, 06:37 PM   #11
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
If you didn't use leading zero's you could use "mkdir foo{1..1000}".
 
Old 02-05-2008, 10:33 AM   #12
Count Zero
Member
 
Registered: Feb 2008
Distribution: Debian wheezy
Posts: 130

Original Poster
Rep: Reputation: 15
Talking

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!
 
Old 07-06-2008, 02:59 AM   #13
Count Zero
Member
 
Registered: Feb 2008
Distribution: Debian wheezy
Posts: 130

Original Poster
Rep: Reputation: 15
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.
 
Old 07-06-2008, 03:34 AM   #14
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by Count Zero View Post
$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.
 
Old 07-06-2008, 03:46 AM   #15
Count Zero
Member
 
Registered: Feb 2008
Distribution: Debian wheezy
Posts: 130

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by colucix View Post
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
 
  


Reply

Tags
bash, create, directories, multiple



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script to create sequential, numbered directories ericcarlson Linux - Software 4 01-08-2010 09:43 AM
Change text in multiple files in multiple directories vivo2341 Linux - General 5 11-27-2006 08:16 PM
Script to create multiple directories under users homes shawnbishop Linux - Software 4 03-30-2006 08:11 AM
Searching multiple directories and sub directories for a file jeep99899 Linux - Newbie 2 10-13-2005 12:23 PM
BASH scripting browsing multiple directories PokerFace Programming 3 10-02-2002 12:50 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration