[how to] Create multiple directories using terminal

rado84

Well-Known Member
Joined
Feb 25, 2019
Messages
757
Reaction score
623
Credits
4,705
For quite some time I was using Total Commander for Windows because it has the ability to create multiple directories. But I finally found an easy way to do that without Total Commander, so I decided to share it with everyone who don't know how. For Linux gurus it may be trivial but a newbie or someone like me who's between "newbie" and "intermediate" will have to gut the internet in order to find the answer.

For this example I'll use directories that normally don't exist in Linux by default, so that you can see more clearly what's happening.
Just open terminal from the menu and type:
Code:
mkdir -p ~/windows/appdata/roaming/local
and press enter. This will create the directory 'windows' in your home folder and inside 'windows' there will also be the directories 'appdata', 'roaming' and 'local'. The '-p' command stands for "parent", I think. Using the '-p' command you can create as many directories as you like, there's no limit of the number.

Also, if you already have existing folders and you wanna save some typing, you can open terminal in folder where you wanna create multiple directories and type the same command. For instance, my primary NTFS partition is located in '/media/rado/Drive_D'. If I want to create several additional directories inside the "BOOKS" folder on the NTFS partition, I'll just use the file manager to navigate to that folder, open terminal inside (for PCManFM this can be done by pressing F4 inside the folder) and then type the '-p' command:

mkdir-p.png
 


Ah, so you're talking about using the -p (--parents) flag to make multiple, nested directories that do not already exist.

You can also use shell expansions, to make more complex directory structures like this:
Code:
mkdir -p ./path/to/somedir/directory{01..10}/directory{A..Z}

Which would make ten sub-directories at ./path/to/somedir/ named directory01 to directory10. And each of those directories contains 26 sub-directories named directoryA to directoryZ.

Note: If you ran the above command from your home directory - you can remove the additional subdirectories by using the commands:
Code:
cd
rm ./path/ -r

Using cd on its own should take us back to our home directory - in case you navigated inside the directory tree we just made.
And the rm command with -r will recursively delete the sub-directories inside ./path

NOTE: Always use rm -r with care. I always put the -r flag at the end of the command.
After typing rm and the item, or list of items to delete, I visually check the paths are correct, before adding the -r flags and then visually check the command a final time before hitting the enter key - just to make sure we aren't about to accidentally delete something we didn't intend to!

Now the question remains - why would you want to use a shell expansion like this? The example I used above was extremely random. Is there a practical use for this kind of command? Well, yes - there is!

By using a shell expansion like this, you can quickly create complex directory trees with a single command. So, as a completely contrived example - lets say you have a spare NAS device knocking about and you wanted users on your home network to be able to store and organise their ebook and music collections in one place. You could navigate to the device and then set up a directory structure for all of the users using a single command like this:
Code:
mkdir -p ./collections/{jason,oscar,oliver}/{books,music}/{A..Z}

This would create a collections directory, with a sub-directory for each user.
Each users directory will have two sub-directories called "music" and "books". And in turn, each "music" and "books" directory would have 26 sub-directories named A to Z. So each user can store and organise their collections alphabetically by artist / author name.

And if you tried running that command - you can remove the directory tree we just added by:
cd-ing back to whatever directory the collections directory was added to. Then use:
Code:
rm ./collections -r

I'll leave you lot to CAREFULLY have a play with that!
 
Last edited:
Very good posts @rado84 , @JasKinasis - thanks.

Also, regarding using 'rm', the 'i' (lowercase i) or 'I' (uppercase I) [interactive mode] option may be useful or even wise.
Will prompt user before carrying out deletion.

From the Man page:
-i prompt before every removal (note: could get tedious)
-I prompt once before removing more than three files, or when
removing recursively; less intrusive than -i, while still giving
protection against most mistakes

I believe I recall reading somewhere it may even be a good idea to make 'rm -I' an alias for 'rm' for us forgetful folks :)
 
You can also use shell expansions, to make more complex directory structures like this:
Code:
mkdir -p ./path/to/somedir/directory{01..10}/directory{A..Z}

Which would make ten sub-directories at ./path/to/somedir/ named directory01 to directory10. And each of those directories contains 26 sub-directories named directoryA to directoryZ.
Just out of curiosity - what would you write in place of {A..Z} if you wanted to have directory names with random generated numbers (RGN) with 20 digits? Like those in Windows when you install Visual C++ and you end up with multiple RGN dirs on the drive they're stored.

Code:
rm ./collections -r

If I read this right, it should remove all subdirs in /collections leaving the /collections itself intact? Or am I wrong?
 

Members online


Top