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
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.
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 .
When you see the new folder icon, you can rename the folder to a name of your choice.
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
2. -v or -verbose : The -v command adds verbose messages to each directory created.
mkdir - v file1 file2
3. --version : The --version command outputs license version details.
mkdir --version
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:
- rm
- 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
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:
- -f: Force deletion of files and folders from the directory
- -i: Request permissions before deleting folders
- -r: Delete stored directories and files recursively
- -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
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.
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.
You should read it
- How to Unzip Files in Linux
- Top 7 best Linux terminal emulators
- 14 interesting Linux commands in Terminal
- How to use Guake Terminal in Linux
- How to run 2 or more Terminal commands at the same time on Linux
- How to back up and restore Linux Terminal history
- How to Launch Programs from Command Line on Linux
- How to install dictionaries in Linux Terminal
- How to Create and Edit Text File in Linux by Using Terminal
- How to Unzip Files on Linux
- How to open a folder on a Mac using a command in Terminal
- How to play Game Boy games on Linux Terminal
Maybe you are interested
What to do when open command window here does not appear?
How to switch users on the Linux command line
How to fix Mac Homebrew error 'zsh: command not found: brew'
What is the Command key on Windows?
How to use read command in Linux
Basic Linux commands everyone needs to know - Operations on Linux are much faster