Tell me more ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

In my scenario, I have some photos and I want to keep them seperate. At present I am doing mkdir gallery1 gallery2 gallery3 gallery4 gallery5 gallery6, this is a pain. I think we can do it easily as well. Suppose I want to make directories from gallery7 to gallery50. How would I do that?

share|improve this question

4 Answers

up vote 11 down vote accepted

With brace expansion.

mkdir gallery{1..50}
share|improve this answer

You can use brace expansion in a bash script. The following creates the directories gallery7 to gallery50 in the current directory:

#!/bin/bash
for i in {5..70}
do
   mkdir "gallery$i"
done
share|improve this answer
You are right, but its cumbersome to create a script for tasks like this. :) Specially if shorter solutions already exists. – Santosh Kumar Jan 4 at 15:12

While there are already fine answers, and you have already started, I'll throw mine in anyway.

I always prefer to make numerically sequenced files with leading zeros, like so:

mkdir $(printf "gallery%02d\n" {1..50})

This will give you directories gallery01 through gallery50. This helps to keep them nicely aligned and in the correct order when sorting.

share|improve this answer
3  
In newer version of bash you can do mkdir gallery{01..50} to achieve the same result. – otokan Dec 18 '12 at 13:29
@otokan, wow, thanks! tested in bash 4.2.39(1) in fedora 18, it works! – LiuYan 刘研 Dec 18 '12 at 14:33

As the already mentioned brace expansion isn't supported by all shells you can also use seq from the coreutils instead:

for i in $(seq -w 1 50)
do
    mkdir "gallery$i"
done

Option -w is for equal width, i.e. creating gallery01, gallery02 etc.

share|improve this answer
No need for the loop: seq -f 'mkdir gallery%02g' 50 | sh. – manatwork Dec 18 '12 at 14:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.