How to create and delete folders in Linux Terminal

While there is always the option of creating folders manually using a file manager, many advanced users may prefer to use a terminal window to create and navigate through files and folders on their machine.

If you're a Linux enthusiast and want to master creating and managing folders from the command line, here's what you need to know.

How to create folders using command line

The mkdir command holds the key to all your directory creation tasks. You can create one or more folders in terminal with one line of code. Here's how the mkdir command on Linux works:

1. Create a single directory on Linux

Here's how you can use the mkdir function to create a directory:

mkdir [option] 

For example, if you want to create a new directory, you can use the following command:

mkdir MUO

The above command creates a new MUO folder in the current location. To navigate to the MUO directory via the command line, you can use the cd command:

cd ./MUO

How to create and delete folders in Linux Terminal Picture 1

 

The cd command is a very flexible way to change directories in the Linux terminal.

2. Create multiple folders on Linux

The mkdir function is very flexible, allowing you to create multiple directories with just one command. For example, to create multiple folders via command line, use the code below:

mkdir .

To create 3 directories, here is how you can use the mkdir command:

mkdir MUO_sample1 MUO_sample2 MUO_sample3

When you run the above command, the program will create 3 folders: MUO_sample1, MUO_sample2 and MUO_sample3. You can run the ls command to list newly created directories.

How to create and delete folders in Linux Terminal Picture 2

The Linux directory structure is complex, so it's best to familiarize yourself with parent and child directories to avoid unnecessary hurdles.

 

3. Create folders directly

As a Linux user, you can create folders directly at the desired location. You do not need to navigate to the location or use the mkdir function in the terminal.

All you need to do is go to the folder/location where you want to create a new folder/folder and right click.

From the list menu, click on the New Folder option .

How to create and delete folders in Linux Terminal Picture 3

When you see the new folder icon, you can rename the folder to a name of your choice.

How to create and delete folders in Linux Terminal Picture 4

 

Rest assured, if you want to check the directory status from a terminal window, you can still use the cd command to navigate to a specific location and run the ls command to list the contents.

mkdir options

You can use several options with the mkdir command, which help you enhance the command's existing utility. You can use the commands listed below:

1. -p or -parents : The -p command is useful when creating parent folders and child folders together. If the specified parent folder already exists, it will automatically create subfolders.

mkdir -p MUO/MUO_CHILD

How to create and delete folders in Linux Terminal Picture 5

2. -v or -verbose : The -v command adds verbose messages to each directory created.

mkdir - v file1 file2

How to create and delete folders in Linux Terminal Picture 6

3. --version : The --version command outputs license version details.

 

mkdir --version

How to create and delete folders in Linux Terminal Picture 7

Delete empty and non-empty Linux folders

Finally, once you have created folders on your Linux operating system, you may want to delete those you no longer need. You can delete folders on your Linux operating system in two ways:

  1. rm
  2. rmdir

Let's discuss both these methods in detail!

If you want to delete empty and non-empty folders, you can use the rm command as follows:

1. Check the contents of the folder

Before deleting a folder, you should check its contents. To do so, navigate to the folder's location using the cd command.

cd ./MUO

In the example above, MUO is a folder on the Ubuntu Desktop.

2. List directory contents

Once you are in the directory's location, use the ls command to list the directory's contents.

ls

How to create and delete folders in Linux Terminal Picture 8

The command lists all the contents of a directory. You will see a list of all files stored in this folder on your screen.

3. Delete the folder via Terminal

Enter the command rm, followed by the various commands and the name of the folder you want to delete.

rm -option 

You can use the ls command again to confirm the directory is no longer there.

Here are some of the most commonly used options with the rm command:

  1. -f: Force deletion of files and folders from the directory
  2. -i: Request permissions before deleting folders
  3. -r: Delete stored directories and files recursively
  4. -d: Delete empty directory

You can even remove multiple folders in one go. For example, when you want to delete the folders MUO_sample1, MUO_sample2, and MUO_sample3 with one command, here's how you can do it:

rm -d MUO_sample1 MUO_sample2 MUO_sample3

How to create and delete folders in Linux Terminal Picture 9

 

There is an alternative method you can use to delete empty folders. You can use the rmdir command to quickly delete directories as follows:

rmdir 

To delete the MUO3 folder when it does not have any files/subfolders in it, you can use the rmdir command as shown below:

rmdir MUO3

However, if the directory is not empty, you will receive an error message whenever you try to delete the directory using the rmdir command.

How to create and delete folders in Linux Terminal Picture 10

Note : Use rm and rmdir commands with care because deleted directories cannot be retrieved.

4. Delete folder directly

According to the way you create the folder, you can even delete that folder/folder directly. Select the folder you want to delete and press the Delete button on the keyboard. Make sure you check the folder's contents before deleting it, as this method is permanent.

5 ★ | 1 Vote

May be interested

  • 7 Ways to Restart Linux from Terminal7 Ways to Restart Linux from Terminal
    restarting linux from the terminal may seem difficult, but don't worry, it's quite simple once you know the right commands. restarting linux is necessary after updating, troubleshooting, or performing system maintenance.
  • How to Create and Edit Text Files Using Terminal on LinuxHow to Create and Edit Text Files Using Terminal on Linux
    in this article, tipsmake will show you how to use terminal software on linux to create text files. after creating the file, you can use pre-installed linux editing software to edit the text.
  • 5 interesting entertainment games in Linux Terminal5 interesting entertainment games in Linux Terminal
    if you are looking to learn how to use the linux terminal, a great approach for beginners is to install and play some simple games.
  • How to run 2 or more Terminal commands at the same time on LinuxHow to run 2 or more Terminal commands at the same time on Linux
    if you use linux, you probably know how to use useful commands to work with files, install software and launch programs. however, one thing you don't know is that you can still run multiple commands at the same time.
  • 14 interesting Linux commands in Terminal14 interesting Linux commands in Terminal
    terminal is a very powerful tool, but it can become 'interesting' through a few bash commands that quantrimang will introduce to you later. let's follow up and find out because some commands are quite useful.
  • 6 interesting terminal commands on Mac6 interesting terminal commands on Mac
    terminal is a handy utility, often found on unix-based computers, such as linux and macos. there are many interesting things to do in the terminal. regardless of your experience, you can use these great terminal commands on mac.
  • 18 Interesting Linux Commands in Terminal18 Interesting Linux Commands in Terminal
    terminal is a very powerful tool, but it can be made 'interesting' through a few bash commands that tipsmake.com will introduce to you below. let's follow and learn because some commands are quite useful.
  • Use CMD to delete large folders on WindowsUse CMD to delete large folders on Windows
    large directories on the system are one of the culprits that consume a lot of system space. a lot of folders that you create, after deleting, will come back later. maybe it's junk folders that you try to remove.
  • How to spell check in Linux TerminalHow to spell check in Linux Terminal
    linux also has a command line spell checker utility in terminal. the utility is called aspell and is installed by default on ubuntu. this is how you can use aspell to check spelling in a terminal.
  • How to search Google from Linux terminal with GooglerHow to search Google from Linux terminal with Googler
    googler is a terminal tool that allows you to easily run google searches from your linux terminal, with lots of options to help you find exactly what you're looking for.