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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
|
02-03-2008, 11:46 AM
|
#1
|
|
Member
Registered: Feb 2008
Distribution: Debian wheezy
Posts: 130
Rep:
|
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.
|
|
|
|
02-03-2008, 11:59 AM
|
#2
|
|
Moderator
Registered: May 2001
Posts: 24,970
|
Quote:
Originally Posted by Count Zero
experimenting without any success at all.
|
Show us what you've tried?
|
|
|
|
02-03-2008, 12:03 PM
|
#3
|
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,797
|
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
|
|
|
|
02-03-2008, 12:13 PM
|
#4
|
|
Member
Registered: Feb 2008
Distribution: Debian wheezy
Posts: 130
Original Poster
Rep:
|
Yes!
Quote:
Originally Posted by pixellany
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!
|
|
|
|
02-03-2008, 12:32 PM
|
#5
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,899
|
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)
|
|
|
|
02-03-2008, 03:04 PM
|
#6
|
|
Member
Registered: Feb 2008
Distribution: Debian wheezy
Posts: 130
Original Poster
Rep:
|
Quote:
Originally Posted by colucix
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.
|
|
|
|
02-03-2008, 03:58 PM
|
#7
|
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,797
|
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.
|
|
|
|
02-03-2008, 04:16 PM
|
#8
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,899
|
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
|
|
|
|
02-03-2008, 04:33 PM
|
#9
|
|
Member
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 714
Rep:
|
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
|
|
|
|
02-03-2008, 06:26 PM
|
#10
|
|
Member
Registered: Sep 2003
Location: Fort worth, TX
Distribution: Debian testing 64bit at home, EL5 32/64bit at work.
Posts: 187
Rep:
|
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!
|
|
|
|
02-03-2008, 06:37 PM
|
#11
|
|
Moderator
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
If you didn't use leading zero's you could use "mkdir foo{1..1000}".
|
|
|
|
02-05-2008, 10:33 AM
|
#12
|
|
Member
Registered: Feb 2008
Distribution: Debian wheezy
Posts: 130
Original Poster
Rep:
|
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!
|
|
|
|
07-06-2008, 02:59 AM
|
#13
|
|
Member
Registered: Feb 2008
Distribution: Debian wheezy
Posts: 130
Original Poster
Rep:
|
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.
|
|
|
|
07-06-2008, 03:34 AM
|
#14
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,899
|
Quote:
Originally Posted by Count Zero
$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.
|
|
|
|
07-06-2008, 03:46 AM
|
#15
|
|
Member
Registered: Feb 2008
Distribution: Debian wheezy
Posts: 130
Original Poster
Rep:
|
Quote:
Originally Posted by colucix
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
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 04:11 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|