How To Quickly Create Files Inside Nested Directories In Linux
Howdy, Linux users! I hope you’re enjoying Christmas. This brief guide explains how to quickly create files inside nested directories in any Unix-like operating system. It is easy, isn’t? Yeah, we can create multiple directories and/or a hierarchy of directories (directories within directories) using ‘mkdir’ directory. Also, we can create multiple files using ‘touch’ command. 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
Right? Yes, we are.
Also, 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/ 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/ 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 using command:
touch file1.txt file2.txt file3.txt file4.txt file5.txt
To create files under existing nested directories, we use:
Now dir3 has two items, i.e dir4 and file3.txt. You can verify it using command:
ls -R dir1/
Sample output of the above command:
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.
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.
sudo cp bin/advance /usr/local/bin/ad
Finally, make it executable using command:
sudo chmod +x /usr/local/bin/ad
Done! It is time to test it. Let us create a hierarchy of directories.
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!
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
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. More good stuffs to come. Stay tuned!
Thanks for stopping by!
Help us to help you:
- Subscribe to our Email Newsletter : Sign Up Now
- Support OSTechNix : Donate Via PayPal
- Download free E-Books and Videos : OSTechNix on TradePub
- Connect with us: Facebook | Twitter | Google Plus | LinkedIn | RSS feeds
Have a Good day!!