How to Create a Subdirectory Under a Directory in Linux

In Linux distributions, the mkdir command is considered a powerful command among Linux users to create single or multiple directories at a time. Sometimes, while working with multiple types of data, we need to make subdirectories or a directory tree under a directory; for this, the “make directory (mkdir)” command tool comes with an effective solution. You can create several subdirectories by running a single command.

This guide will mention all possible examples of creating subdirectories under a directory to learn it in a better way.

The mkdir command is a powerful command-line utility in Linux systems to create a single directory or multiple directories. It is also helpful to make multiple subdirectories under a single one or to create a directory tree. Throughout this guide, we have executed multiple examples to learn the mkdir command and created subdirectories under a directory in Ubuntu 22.04.

How to Create a Subdirectory Under a Directory in Linux

There’s nothing difficult in creating a subdirectory in the directory; all you need to do is follow the steps.

Step 1: Open the terminal and make a parent directory first if you don’t have any. This would be done using the given command:

In the above command, the -p parameter is used to create a parent directory if the directory doesn’t already exist; and parent_folder is the directory name.

(You can use either -p or –parent with a directory name).

Step 2: Now, you can create a subdirectory within the parent directory by the use of the following command in a terminal. Like in parent_folder, let’s create a subdirectory with the name child_directory with the help of the mentioned command:

mkdir parent_folder/child_directory

Step 3: Use the ls command to check if the subdirectory has been created under the parent directory. First, use the cd command to navigate toward parent_folder:

cd ~<parent_directory_name>

In my case, my parent directory is already created in the Home directory, so I don’t need to use the cd command.

Run the ls command to display the list of all directories present in the Home directory:

Step 4: Move the current directory to the parent_folder directory using the cd command:

Step 5: So, the current working directory is parent_folder; use the ls command to check if the child directory we created in Step 2 is present in it:

How to Create Multiple Subdirectories within a Directory

You can also make several subdirectories in a single directory using the brace expansion.

The brace expansion {….} helps to generate lists of strings and is a very useful command in Linux distributions; one of its uses is you can make various folders within a single directory.

Let’s check it practically by running the mkdir command tool with brace expansion.

sudo mkdir -p test/{F1,F2}

Move the current directory to the test directory using the cd command:

Execute the ls command to print subdirectories in a terminal:

Similarly, nest brace expansion can be used to create multiple subdirectories under a subdirectory. For example, if you want to create subdirectories in the F1 directory, you can do so using nested brace expansion:

sudo mkdir -p test/{F1/{file1,file2},F2}

Change the current directory to the F1 directory using the cd command:

Again, run the ls command to display the F1 subdirectories:

Conclusion

The mkdir command is a powerful command-line utility in Linux systems to create a single directory or multiple directories. It is also helpful to make multiple subdirectories under a single one or to create a directory tree. Throughout this guide, we have executed multiple examples to learn the mkdir command and created subdirectories under a directory in Ubuntu 22.04.

source

Leave a Comment