Home FAQ How To Quickly Create Files Inside Nested Directories In Linux
Quickly Create Files Inside Nested Directories In Linux

How To Quickly Create Files Inside Nested Directories In Linux

By sk

This brief guide explains how to quickly create files inside nested directories in Unix-like operating systems. It is easy, isn't? Yeah, we can create multiple directories and/or a hierarchy of directories (directories within directories) using 'mkdir' directory. And, we can create multiple files using 'touch' command as well. Also, I know an another way. Read on to find out what it is!

Quickly Create Files Inside Nested Directories In Linux

Usually, we create multiple directories at once using command:

$ mkdir dir1 dir2 dir3 dir4 dir5


$ mkdir dir{1,2,3,4,5}

Right? Yes, we do.

And, we create a hierarchy of directories (nested directories) as shown below.

$ mkdir -p dir1/dir2/dir3/dir4/dir5

Here, "-p" flag will create parent directory if it is not created already. If you also want to view the directory creation process as they created, use "-v" flag like below.

$ mkdir -pv dir1/dir2/dir3/dir4/dir5

The above commands will create directory(s) recursively inside a non-existent directory(s). You can use the 'tree' command to view the directory structure.

$ tree dir1/
└── dir2
 └── dir3
 └── dir4
 └── dir5

4 directories, 0 files

As you can see, dir1 is the parent directory of all other directories inside it. The directories inside dir1 are called child directories of dir1 and parent directories of the subsequent directories. To put this simply, dir1 is the parent directory of all. dir2 is the child directory of dir1 and parent directory of dir3 and so on.

The contents of the new hierarchy of directories can be viewed using 'ls' command like below:

$ ls -R dir1/





You can also use 'du' command to display the directory tree.

$ du dir1/
4 dir1/dir2/dir3/dir4/dir5
8 dir1/dir2/dir3/dir4
12 dir1/dir2/dir3
16 dir1/dir2
20 dir1/

You know now how to easily create multiple directories and hierarchy of directories. Like wise, we can create multiple files at once using command:

$ touch file1.txt file2.txt file3.txt file4.txt file5.txt

To create files under existing nested directories, we use:

$ touch dir1/dir2/dir3/file3.txt

Now dir3 has two items, i.e dir4 and file3.txt. You can verify it using command:

$ ls dir1/dir2/dir3/


$ ls -R dir1/

Sample output of the above command:



dir4 file3.txt



Please note that you can't create files under non-existent directories using 'touch' command. You should first create the directories and then create files.

As you can see in the above examples, creating multiple files and directories isn't that difficult. We can do it with some one-liner commands as described above. However, I'd like to introduce yet another simple script called "terminal-AdvancedNewFile" or shortly "ad". It makes the process of creating multiples files and directories faster and easier than using 'touch' and 'mkdir' commands.

Git clone 'ad' repository:

$ git clone https://github.com/tanrax/terminal-AdvancedNewFile.git

This command clone all contents of 'ad' repository in a folder named "terminal-AdvancedNewFile".

Go to that directory and copy the script to your local bin/ folder.

$ cd terminal-AdvancedNewFile/
$ sudo cp bin/advance /usr/local/bin/ad

Finally, make it executable using command:

$ sudo chmod +x /usr/local/bin/ad

Alternatively, you can install it using Pip package manager like below.

$ pip3 install --user advance-touch

To update it, just run:

$ pip3 install --user --upgrade advance-touch

Done! It is time to test it. Let us create a hierarchy of directories.

$ ad dir1/dir2/dir3/dir4/dir5

Just use the / (forward slash) after each directory to create any number of nested directories. It's that simple.

I want to create a file named file5 inside dir5. How to do it? This will do!

$ ad dir1/dir2/dir3/dir4/dir5/file5

Did you notice? I didn't specify any extension to the file5. If you do the same process using mkdir command, it will create a directory called file5. But, ad automatically recognizes from the file name (file5) and created the file even if I didn't specify the extension. Without ad utility, you will have to create the directory first using 'mkdir' command and then create files using 'touch' command as shown below.

$ mkdir -p dir1/dir2/dir3/dir4/dir5
$ touch dir1/dir2/dir3/dir4/dir5/file5.txt

Or, combine the two commands as a single command like below:

$ mkdir -p dir1/dir2/dir3/dir4/dir5 && touch dir1/dir2/dir3/dir4/dir5/file5.txt

Hope this helps.


Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like

1 comment

shifu August 9, 2019 - 5:00 pm

recursively create directory is very easy: mkdir -p foo/bar/dir_name


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