Creating, Deleting, and Managing Directories on Linux

This tutorial explains how to perform some basic tasks such as creating, deleting, moving, and managing folders using command line from the Terminal.

Linux filesystem is a hierarchical tree, with the top level directory named /, or a root directory. Every other directory is a subdirectory that sits beneath this top directory. Most of the Linux distributions directory structure follows the guidelines set by the Filesystem Hierarchy Standard, or FHS, made back in 1994 to prevent the confusion between different directory layouts in different distributions.

In Microsoft Windows, we have disks that are named C:, D:, and so on, and there is no directory above them. In Linux, even if we have different drives, we still have one root directory. Additional disks (or more general: devices) can be attached or mounted to some of its subdirectories.

For example, we may add a disk, format it accordingly and mount it to any directory we created, for example, /newdisk. It still is a subdirectory of a root directory.

We must not confuse the root directory with /root/ directory, which is the home folder of user root.

In today’s Learn Linux article, we will teach you how to create, delete, and manage the directories using command lines from the Linux Terminal. It applies to all Linux distributions. There is always the GUI way in Linux too, using right-click contextual menu from the desktop environment which is straight-forward. This guide is for doing things the command line way.

Creating Directories

The simplest way to create a directory in the current directory is by using mkdir command:

[[email protected] ~]# mkdir foss

We just created a folder called foss in our current directory.
We can also specify a directory using an absolute path, for example, to create a directory called foss1 in tmp/ directory, we would use:

[[email protected] ~]# mkdir /tmp/foss1

If we need to create multiple directories at the same command, we can do that as well, the following command will create directories foss2 and foss3 at the same time, in our current directory.

[[email protected] ~]# mkdir foss2 foss3

There is also a little trick, called brace expansion, which can help you create multiple directories following a pattern. The following will create directories foss4, foss5, fossa, and fossb.

[[email protected] ~]# mkdir foss{4,5,a,b}

You can even specify a range inside braces, like the following command, creating foss6foss10 directories.

[[email protected] ~]# mkdir foss{6..10}

The range also applies to letters, so this will work as well:

[[email protected] ~]# mkdir foss{d..f}

I told you that we could use absolute paths when creating directories, but what if a link is missing, for example, we want to create a folder inside a folder that doesn’t exist, called missing?

[[email protected] ~]# mkdir missing/foss11
mkdir: cannot create directory `missing/foss11': No such file or directory

It rightfully reports an error. Fortunately, there is an option to mkdir, that will create that missing directory, and that is -p as in parent. Let’s try again:

[[email protected] ~]# mkdir -p missing/foss11
[[email protected] ~]# ls missing/foss11

It creates not only one, but multiple missing directories if needed. That is all you need to know about creating directories in Linux.

Deleting Directories

We have made quite a mess, and we need to delete some of the foss directories we created.
Using the usual rm that we used delete fields will bring us the following:

[[email protected] ~]# rm foss
rm: cannot remove `foss': Is a directory

To delete it, we need to use -r switch, r as in recursive. If we try that, we’ll succeed.

[[email protected] ~]# rm -r foss/
rm: remove directory `foss'? Y

You can see that the system has asked us to confirm the deletion.

We have successfully deleted an empty directory. If it has some content in it, files or directories, this is a different story. Let’s create a file and a directory inside foss1 directory.

[[email protected] ~]# mkdir foss1/test_dir
[[email protected] ~]# touch foss1/test_file

This is a basic example, but the directory tree that we are trying to delete may contain hundreds or thousands of files or folder.
If we need to remove all of them, without spending the entire day confirming it, we use -f, as in force, parameter.

[[email protected] ~]# rm -rf foss1/

When deleting a directory, you may, or you may not have a slash(‘/’) at the end of the directory name. I propose that you don’t use slash. Why?
With rm -rf, you will delete the following directory or directories. If you mistype the command and put an extra space between foss1 and ‘/’, you will delete both foss1 AND ‘/’, a root directory, destroying your whole Linux filesystem without the ability to restore it (most of the times).
This is a death command. So, be very careful! You have been warned. With great power comes great responsibility, as they say.

Moving Directories

Sometimes, we need whole directories moved to another directory. If, for example, we need to move the entire directory foss8 to the /tmp/ directory, we can do it with:

[[email protected] ~]# mv foss8 /tmp/

It should be enough to move; we don’t need to specify the foss8 again after the /tmp/.
You noticed that we did not need to specify -r or similar argument, it automatically moves the whole directory tree.
But there is still one option that is similar to rm, and that is -f.
We have a foss8 directory in our /tmp/ directory, let’s see what happens when we re-create foss8 and try to move it to /tmp/ again.

[[email protected] ~]# mkdir foss8
[[email protected] ~]# mv foss8 /tmp/
mv: overwrite `/tmp/foss8'?

Answer ‘n’ or press CTRL+C to escape the command.
You see that it asks us to overwrite the existing directory, even though both are empty. To avoid this, use -f option to force overwriting.

[[email protected] ~]# mv -f foss8 /tmp/

Renaming Directories

You probably know already that there isn’t a separate command to rename files in Linux. Since directories in Linux are only a special type of file, the same principle applies to directories as well. Renaming is done with mv command, followed by source directory and a destination directory name. So if we need to rename directory foss10 to foss101, we will issue the following command:

[[email protected] ~]# mv foss10 foss101

Cleaning the mess

Now, to clean everything that we have created in one command, use * to specify multiple directories starting with foss, and also ones in /tmp/ directory.

[[email protected] ~]# rm -rf foss* /tmp/foss*
Jasenko Krejic
Hello, My name is Jasenko Krejić, I live in the beautiful town of Banjaluka, Bosnia, and Herzegovina. After wrapping up my master in telecommunications, I have been working with computer systems. A decade ago, I had my first encounter with Linux and immediately fell in love with it because of it being an open-source! I work in an enterprise-level company, but I always try to stay ahead and do various other projects, and learn new technologies. Part of learning is sharing knowledge. I consider it a part of my learning path. I hope that in my articles, I have aggregated my knowledge, explained the material as simple as possible and that I have sparked your curiosity to learn Linux.


Please enter your comment!
Please enter your name here




cool and fun linux commands
The real purpose of this article is to help Linux newbies get comfortable and confident with the Linux command-line.  While knowledge and comfort of the Linux GUI allow great power, it is the mastery of the command line, or CLI, affords the Linux user unlimited power and certifies them as a Linux power user well on their way to becoming an expert.
MX Linux 19 Features and Desktop Tour
MX Linux is a Linux distribution based on Debian stable and using core antiX components. The distributions MX Tools is very popular among users and combined with other several ready-to-use tools, it is great for users who prefer to tweak their distro to their liking. In this video, we will take you through the features tour showing casing MX Linux 19.