This brief guide explains how to quickly create files inside nested directories using "terminal-AdvancedNewFile" tool in Linux and Unix-like operating systems.
Table of Contents
1. Create multiple directories and files
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.
1.1. Create multiple directories using mkdir command
Usually, we create multiple directories at once using mkdir command like below:
$ mkdir dir1 dir2 dir3 dir4 dir5
Or,
$ mkdir dir{1,2,3,4,5}Right? Yes!
And, we create a hierarchy of directories (nested directories) as shown below.
$ mkdir -p dir1/dir2/dir3/dir4/dir5
Here, "-p" flag will create the parent directory if it is not created already.
If you want to view the directory creation process (verbose output) as they created, use -v flag like below.
$ mkdir -pv dir1/dir2/dir3/dir4/dir5
The above commands will create directories recursively inside a non-existent directory(s).
To verify if the directories are created, simply use the 'tree' command to view the directory structure.
$ tree dir1/ 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 in other words, 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/ dir1/: dir2 dir1/dir2: dir3 dir1/dir2/dir3: dir4 dir1/dir2/dir3/dir4: dir5 dir1/dir2/dir3/dir4/dir5:
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.
1.2. Create multiple files using touch command
To create multiple files at once using touch command, run:
$ touch file1.txt file2.txt file3.txt file4.txt file5.txt
Similarly, we can create files under existing nested directories using command:
$ touch dir1/dir2/dir3/file3.txt
Now dir3 has two items, i.e dir4 and file3.txt. You can verify it using ls command:
$ ls dir1/dir2/dir3/
Or,
$ ls -R dir1/
Sample output:
dir1/: dir2 dir1/dir2: dir3 dir1/dir2/dir3: dir4 file3.txt dir1/dir2/dir3/dir4: dir5 dir1/dir2/dir3/dir4/dir5:
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.
2. Install terminal-AdvancedNewFile in Linux
The terminal-AdvancedNewFile utility is a python script to quickly create folders and files like a pro.
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.
3. Create files inside nested directories in Linux using terminal-AdvancedNewFile utility
To create a hierarchy of directories using ad, run:
$ 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? The following command 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 would create a directory called file5.
But, ad script automatically recognizes it from the file name (i.e. 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
Conclusion
In this guide, we learned how to create multiple directories and files using traditional Linux commands namely mkdir and touch. We also looked at how to create files inside nested directories using a Python script named "terminal-AdvancedNewFile".
Resource:

3 comments
recursively create directory is very easy: mkdir -p foo/bar/dir_name
i think the lesson needs an update since the installation process of the command is different now.
What installation?