Home Command line utilities How To Create Directories From A Text File In Linux
Create Directories From A Text File In Linux

How To Create Directories From A Text File In Linux

By sk
734 Views

As you already know, we can easily create a single or group of directories at a time using mkdir command. It is also possible to create nested directories (a directory inside a directory) with mkdir command. Also, there are other ways to create directories too. This brief tutorial describes how to create directories from a text a file from command line in Linux.

Create Directories From A Text File In Linux

We usually create single directory using command:

$ mkdir dir1

The above command will create a directory called "dir1" in the current working directory.

To create multiple directories, we use:

$ mkdir dir1 dir2 dir3

Or,

$ mkdir dir{1,2,3}

This command creates three directories namely dir1, dir2, and dir3 in the current working directory.

To create nested directories, we do:

$ mkdir -p dir1/dir2/dir3

The -p option allows you to create parent directory if it is not created already. The above command creates dir2 within dir1 and dir3 within dir2. Here, dir1 is the parent directory to dir2 and dir3. dir2 is the parent directory to dir3. In other words, dir2 is the child directory of dir1 and dir3 is the child directory of dir2 and dir1.

I thought it was only way to create directories from commandline until I came to know about the command xargs. For those wondering, xargs is a Linux command which is used to build and execute command lines from standard input.

Now, let us get back to our topic which is creating directories from a text file using xargs.

Say for example, I want to create a the following directory structure.

Study/Ebooks
Tutorials/Videos
Linux_Commands
Entertainment/Movies/Tamil
Entertainment/Movies/English
Entertainment/Songs
Personal/Bills
Games
Miscellaneous

Any idea how to create this directory structure from a file? No problem. Just put name of the directories one by one in a text file, say mydirectories.txt, as shown below.

Directory structure

Directory structure

Then, run the following command to create the directories.

$ xargs -I {} mkdir -p "{}" < mydirectories.txt

You can verify whether the directories have been created as you wanted using tree command.

$ tree 
.
├── Entertainment
│   ├── Movies
│   │   ├── English
│   │   └── Tamil
│   └── Songs
├── Games
├── Linux_Commands
├── Miscellaneous
├── mydirectories.txt
├── Personal
│   └── Bills
├── Study
│   └── Ebooks
└── Tutorials
 └── Videos

14 directories, 1 file

For more details, refer man pages.

$ man xargs
$ man mkdir
$ man tree

Related read:

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

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

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More