Home Learn Linux Creating, Deleting, and Managing Directories on Linux

Creating, Deleting, and Managing Directories on Linux

by Kiran Kumar
create delete manage directories linux

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:

[root@fosslinux ~]# 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:

[root@fosslinux ~]# 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.

[root@fosslinux ~]# 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.

[root@fosslinux ~]# mkdir foss{4,5,a,b}

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

[root@fosslinux ~]# mkdir foss{6..10}

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

[root@fosslinux ~]# 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?

[root@fosslinux ~]# 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:

[root@fosslinux ~]# mkdir -p missing/foss11
[root@fosslinux ~]# 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:

[root@fosslinux ~]# 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.

[root@fosslinux ~]# 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.

[root@fosslinux ~]# mkdir foss1/test_dir
[root@fosslinux ~]# 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.

[root@fosslinux ~]# 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:

[root@fosslinux ~]# 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.

[root@fosslinux ~]# mkdir foss8
[root@fosslinux ~]# 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.

[root@fosslinux ~]# 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:

[root@fosslinux ~]# 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.

[root@fosslinux ~]# rm -rf foss* /tmp/foss*

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.