Share your love
Create and remove a directory in Linux in Easy Steps
In this article from the Orcacore, we want to show you how to create and remove a directory in Linux in simple ways with examples. A directory is a location for storing files on your computer. Directories are found in a hierarchical file system, such as Linux, MS-DOS, OS/2, and Unix. So here we go to create a directory in Linux.
Table of Contents
Steps To Create and remove a directory in Linux
To Create and remove a directory in Linux, you must log in to your server and proceed to the following steps. Let’s start by creating a directory.
Step 1 – How to create a directory in Linux?
By using the mkdir command you can create a directory. For example, we create a directory named “orca“:
# mkdir orca
Then, use the ls command to verify it:
ls
In your output, you should see the name of the directory:
Output
orca
How to create multiple directories?
Also, you can create multiple directories by using the following command:
mkdir directory1 directory2 directory3
Important Note: The names of directories should be separated by spaces.
How to create parent directories?
To create parent directories, you can use the mkdir command with the –p switch.
mkdir –p directory 1 directory2 directory3
By using this switch if directory1 doesn’t exist mkdir first makes the directory1 (parent) by itself.
Note: The tree command shows the tree of the directory.
tree directory1
Here are the most commonly used switches of the mkdir command:
- -m, –mode=MODE Set file mode (as in chmod), not a=rwx – umask.
- -p, –parents No error if existing, make parent directories as needed.
- -v, –verbose Print a message for each created directory.
If you don’t have permission to use the mkdir command you need to use Sudo permissions to contact a System Administrator.
Step 2 – How to remove a directory?
By using of rmdir command you can remove your “orca” directory:
rmdir orca
Note: If your directory doesn’t empty you can’t remove it.
Remove Directories with rm
rm is a command-line utility for deleting files and directories. Unlike rmdir, the rm command can delete both empty and non-empty directories.
By default, when used without any option rm does not remove directories. To delete an empty directory, use the -d (–dir) option, and to delete a non-empty directory, and all of its contents use the -r (–recursive or -R) option.
rm -d orca
rm -r orca
Conclusion
At this point, you have learned to Create and remove a directory in Linux in Easy Steps. You can easily use mkdir command to create directories and rm command to remove directories. Hope you enjoy it.
Also, you may like to read the following articles: