ProgrammingThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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.
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!
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:
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.
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
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.