How to Create and Delete Directories in the Terminal

You can create easily directories/folders in Linux using a graphical file manger but if you prefer to use the command line, you can easily create using below methods.

How to Create a Directory With the Command Line in Linux

You can create directories quickly using the mkdir command. It allows you to create both single and multiple directories with a single line of code.

Create a Single Directory on Linux

If you want to create a new directory, you can use the below command format.

mkdir [option] <directory name>

For example,

If you want to create a directory named “CONNECTwww,” run the following command in your terminal:

mkdir CONNECTwww

The above command creates a new CONNECTwww directory within the current location.

Create Multiple Directories on Linux

To create multiple folders via the command line, use the code below:

mkdir <directory name1><directory name2><directory name3>....<directory name_n>

To create three folders, here’s how you can use the mkdir command:

mkdir CONNECTwww1 CONNECTwww 2 CONNECTwww 3

Once you run the above command, the program creates three directories: CONNECTwww1 CONNECTwww 2 CONNECTwww 3.

Create a Parent directory and Sub-directory together

You can use the -p option when you need to create a parent directory along with a sub-directory. If the parent directory already exists, it will automatically create the sub-directory within it. Here’s an example:

mkdir -p CONNECTwww_Parent/CONNECTwww_Child

Create and Delete Directories

This command creates a parent directory “CONNECTwww_Parent” and a subdirectory “CONNECTwww_Child” inside it.

Also Read:  How To Find Resistance Value Using Color Code App

Delete a Directory via the Terminal in Linux

Finally, once you have created directories on your Linux OS, you might want to remove the ones you no longer need. You can delete the directories on your Linux OS in two ways. Using rm and rmdir commands. Let’s discuss both these methods in detail:

Using the rm Command:

rm -option <directory name>

The rm command allows you to delete both empty and non-empty directories. You can use it with various options:

-f: Forcefully remove files and directories.
-i: Request confirmation before deleting.
-r: Recursively remove directories and their contents.
-d: Remove an empty directory.

Example:

rm -f CONNECTwww1 CONNECTwww2 CONNECTwww3

There is an alternate method you can use to delete empty directories. You can use the rmdir command to remove the directory quickly, as follows:

rmdir <directory name>

To remove the CONNECTwww directory when it does not have any files/subdirectories in it, you can use the rmdir command as below:

rmdir CONNECTwww

However, if the directory isn’t empty, you will get an error message whenever you try to delete the directory with the rmdir command.

If you liked this article, please subscribe to our YouTube Channel. You can also stay connected with us on X (Twitter) and Facebook.



Leave a Reply