Linux: create a complex directory structure with repeated directory pattern in one line!

Ok! the title might be a bit confusing but i will explain you everything in a few lines, imagine the following scenario! you need to create…

Linux: create a complex directory structure with repeated directory pattern in one line!
Photo by niko photos on Unsplash
Join Medium with my referral link - Konstantinos Patronas
As a Medium member, a portion of your membership fee goes to writers you read, and you get full access to every story…

Ok! the title might be a bit confusing but i will explain you everything in a few lines, imagine the following scenario! you need to create directories for the new company project, each project has directories for the development team and each subdirectory of these directories need to have the same naming! for example:

./project 
├── dev 
│   └── src 
├── prod 
│   └── src 
└── test 
    └── src

To create with the hard way you need to create each upper level dir (dev,prod,test) and then go to each directory and create the src dir, lets see how we can perform this directory structure in one command!

Enter the following:

mkdir -p ./project/{prod,dev,test}/src

What this directory did is that for each element in the braces created the src directory! isnt that cool!? to verify the structure enter

tree ./project  
./project 
├── dev 
│   └── src 
├── prod 
│   └── src 
└── test 
    └── src 
 
7 directories, 0 files

Thats awesome! appart that we impressed our friends with this oneliner we saved much time and potential mistakes might happen in such tasks! lets see something more complex! Assume that we have a new requirement and we need to add two extra directories under the dev,prod and test directories, the new directories will called bin and doc

mkdir -p ./project/{prod,dev,test}/{doc,bin}

What this command did is that created sub directories doc and bin for each of the directories prod, dev and test, to verify this lets run tree again

% tree ./project                               
./project 
├── dev 
│   ├── bin 
│   ├── doc 
│   └── src 
├── prod 
│   ├── bin 
│   ├── doc 
│   └── src 
└── test 
    ├── bin 
    ├── doc 
    └── src 
 
13 directories, 0 files

Isnt that cool? too much work done in little effort!. Oh No! its 5 minutes before your shift ends and you have another urgent request! they need another src,bin,doc dir on each dev,prod and test dir with the suffix of _v2

mkdir -p ./project/{prod,dev,test}/{src,bin,doc}_v2

As we can see since the suffix is the same for each dir we can just append it after the closing brace! to verify this we use tree command again!

% tree ./project                                      
./project 
├── dev 
│   ├── bin 
│   ├── bin_v2 
│   ├── doc 
│   ├── doc_v2 
│   ├── src 
│   └── src_v2 
├── prod 
│   ├── bin 
│   ├── bin_v2 
│   ├── doc 
│   ├── doc_v2 
│   ├── src 
│   └── src_v2 
├── test 
│   ├── bin 
│   ├── bin_v2 
│   ├── doc 
│   ├── doc_v2 
│   ├── src 
│   └── src_v2

Assume that we have another request! to create bin, doc and src dirs with v3 and v4 suffixes! easy!

mkdir -p ./project/{prod,dev,test}/{src,bin,doc}_v{3,4}

What we did is is that for each prod,dev and test dir we created src, bin and doc dirs with suffixes of v3 and v4 which are defined in the last braces set! isnt that cool? if you wanted to do this with traditional for statements it would be very complex and time consuming! this simple and sweet one-liner is quite self explainable without even knowing this mkdir command syntax!

Conclusion

I hope you enjoyed this article as much i enjoyed writing it! learning such one-liners can make you more effective at your work! probably such tricks will not give you a promotion nor you will be the sysadmin/devops of the month but you will have the internal feeling that you did something cool!