Create and remove a directory in Linux

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.

What is a Directory?

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.

Create and remove a directory in Linux

How to create a directory in Linux

By using the mkdir command you can create a dir. For example, we create a directory named “orca“:

root@srv:~# mkdir orca
root@srv:~# ls

How to create multiple directories

You can create multiple directories by the following command:

mkdir directory1 directory2 directory3
directory1 directory2 directory3

Note: The names of directories should be separated by spaces.

How to create parent directories

To create parent directories using the mkdir command pass 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 is some common switch 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.

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

Hope you enjoy this article about how to create and remove a directory in Linux from the Linux command on Orcacore.

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!