How to create multiple directory levels in one command

I’m about to go to move files to my NAS, and I want to create subdirectories named Hercule, Systems, and Documents, and the Hercule directory has severals subdirectories named Saison01 to Saison13. How can I create these directories and subdirectories with one command?

Answer: Use the “-p” option of the Unix/Linux mkdir command. The answer is shown below:

mkdir -p videos/{Hercule/Saison{0{0..9},1{0,1,2,3}},Systems,Documents}

The “-p” option of the Linux mkdir command tells mkdir to “create parent directories as needed” when it’s told to create directories that are multiple levels deep, such as “Hercule/Saison0x”.

When creating directories / folders under Linux, you will use mkdir command. If you want to create multiple directories in one shot, you can use this form of the command:

mkdir -p /var/www/site/{public_html,logs}

That is going to create this structure:

/var/www/site/public_html

and

/var/www/site/logs

At last, if you want numbered folders as my first example, use {0..3} or {0,1,2,3}:

mkdir -p videos/{Hercule/Saison{0{0..9},1{0..3}},Systems,Documents}

An another example with leading zero:

mkdir -p $(seq -f "$NAS/videos/Hercule/Saison%02g" 13)

 

that’s all folks! 😉

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.