7

I'd like to simplify this command:

mkdir -p {fg0,fg1,fg2,fg3,fg4,fg5,fg6,fg7,fg8,fg9,fg10,fg11,fg12,fg13,fg14}

The goal is to create an n number of folders where the numbers increment. I could potentially have 100's of folders, and find it unpractical to add each one individually.

Any thoughts on how to simplify this with a single command?

  • 4
    assuming bash, mkdir -p fg{0..14} – Rui F Ribeiro Jan 5 '18 at 20:11
  • @RuiFRibeiro That did it. Appreciate it – CodeLikeBeaker Jan 5 '18 at 20:12
12

Assuming bash, you can create text sequences using the abbreviation {i..n}

So, I advise doing for having the directories balanced, per @cas sugestion:

mkdir -p fg{00..14}

It will create fg00 ... f01 with newer bash versions 4.x balancing and ordering better the names, and fg0, fg1...with older bash versions (3.x).

If fg0...fg14 is still a requirement, as per your example, than it is indeed:

mkdir -p fg{0..14}
| improve this answer | |
  • 2
    Again, appreciate it. Will accept soon as timer runs out – CodeLikeBeaker Jan 5 '18 at 20:14
  • 1
    or use fg{00.14} - the leading zero causes the output to be zero-padded to the same length. the directory names will sort naturally. – cas Jan 6 '18 at 1:06
  • @cas it is not doing that. $ echo fg{00..14} fg0 fg1 fg2 fg3 fg4 fg5 fg6 fg7 fg8 fg9 fg10 fg11 fg12 fg13 fg14 – Rui F Ribeiro Jan 6 '18 at 1:09
  • works with bash 4.4.12(1)-release. echo {00..14} prints fg00 fg01 fg02 fg03 fg04 fg05 fg06 fg07 fg08 fg09 fg10 fg11 fg12 fg13 fg14. which is nice, i remember it not doing that in the past, and having to use seq instead. – cas Jan 6 '18 at 1:10
  • @cas OSX with 3.2.57....changing the answer. it works both ways. – Rui F Ribeiro Jan 6 '18 at 1:12
0

Something inspired from that I think is good method

mkdir $(seq --format 'fg%.0f' 1 100)
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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