Home » Linux » How to Remove Directory in Linux

About Iulian Timis

My name is Iulian and I graduated Faculty of Electronics, Telecommunications, and Information Technology. My master's degree was in Computer Science, Information Security. I am passionate about technology and the security area. During my career I was working as a Java Developer, on the backend and frontend side, sometimes doing some DevOps tasks.

How to Remove Directory in Linux

1. Introduction

Handle directories to remove/add are the important parts in Linux in terms of structuring items. In this section we will give you a bit of introduction about directory structure in Linux.

A directory is a file that stores file names and the related information. The directories in Linux are a bit different in contrast with Windows. In Linux/Unix systems everything is a file even directories are files. All the input devices like mouse, keyboard, printer, etc are files.

2. Directories structure in Linux

In this section we will present you how the directories look like in Linux with a brief description for each important directory.

Type of files in the Linux System:

  • General files. Most of the ordinary files. It may be an image, video program or even a text file. They can be in different formats like ASCII or Binary.
  • Directory files. This can be a warehouse for other file types. Therefore can be nested directories (subdirectory).
  • Device files. In contrast with Windows where devices like CD-ROM and hard drivers are present as drive letters: G, H, F whereas in Linux system devices are present as files: /dev/sda1, /dev/sda2, etc.

In Windows, files persist in different folders on different data drivers like: D, C, E, on the other hand in Linux files persist in a tree structure starting with the root directory.

Filesystem hierarchy in Linux with the most known directories.

Diagram with the most known directories in Linux.

2.1. Root Directory (/)

Everything in the Linux system is located under the root directory (/). This is similar to Windows where all the files/directories are in the C:/ directory (considered as principle because in Linux we do not have drive letters).

You can go to your root directory by typing: cd /

2.2. Home Directory (~)

The home directory is the directory where you will find yourself when you login. In that directory or subdirectories, you will do the most important work. To go to your home directory you just have to type: cd ~. If you want to go to another home directory just type: cd ~username. To go to the last directory just type: cd –

2.3. Other important directories

These are the most important top-level directories associated with root directory:

/bin #directory for binary or executable programs
/etc #system configuration files
/opt #optional or third party software
/tmp #directory where you store temporary files. Usually clear after reboot
/usr #user related programs
/var #variable data files. Log files and everything else that will be written during normal operation. Log files are located in the /var/log directory.
/boot #contains all the files that needed to boot the system. GRUB boot loader's files and Linux kernels are located in the boot directory. Boot loader configuration is located in the /etc with other configuration files.
/dev #location for the device files like /dev/sda1, /dev/sda2, etc.
/lib #shared libraries and kernel modules
/media #subdirectories for the removal media devices
/mnt #contains the temporary directories that was mounted
/sys #virtual filesystem for modern Linux distribution to store and allow modification for the devices connected to the system.

For more information about Filesystem Hierarchy in Linux, you can check out this link.

3. Removing directories

In this section, we will present to you different ways to remove a directory in Linux.

3.1. Remove empty directories with rmdir

This command is present on various operating systems like: Unix, Windows, macOS, etc. rmdir will remove all empty directories. You can pass different flags to this command. To find the definition and the help manual in Linux you can type in the command line: man rmdir.

If you want to remove all empty directories you can type rmdir <name_of_empty_directory>.

In case the provided directory is not empty you will receive this error on the output:

rmdir: failed to remove 'test1/': Directory not empty

Moreover, if you want to skip the error of a non-empty directory you can pass a flag to the command called: --ignore-fail-on-non-empty.

3.2. Remove directories with rm

The rm command supports removing files and directories. There are multiple flags that you can pass to the command. If you want to find more information about this command you can type in the command line: man rm. In contrast with rmdir, this command can be used to remove files. directories, and also empty directories. This simple command removes a directory (-d stands from the directory, -r stands from recursively): rm -rd <directory>. It will delete the directory with all its content.

In case the directory is not empty you will receive this error on the output:

rm: cannot remove 'test1/': Directory not empty

For files or directories that are write-protected you have to pass the flag -f to the command: rm -fd <directory>. In terms of nested directories (hierarchy of directories), you have to pass also the -r flag which means recursively.

3.3. Remove directories with find

The find command is known for searching different types of files in the Linux system. With this command, you can find files or directories based on a given expression, in a directory hierarchy. As I mentioned above, you can find all information and the supported flags in the manual provided by Linux: man find.

Find command is constructed by: find dir-name criteria action.

dir-name #the directory where to lookup
criteria #define the criteria for which file type to use
action #the action to execute for the given result

Simple command to remove directories with find: find test3 -type d -name 'test3' -delete.

In case the given directory is not empty you will receive this error:

find: ‘/test3’: No such file or directory

If you want to force to remove a non-empty directory with the find command you can type something like this:

 find test3 -type d -name 'test3' -exec rm -rf {} +

3.4. Remove all empty directories

The easy way of removing all empty directories is using find command which is a powerful command that is efficient and fast.

As presented in the previous subsection we can construct our delete command to delete empty directories, something like this:

find test1 -type d -empty -delete

The above command will remove all empty directories that are in the test3 directory, this includes all the empty nested directories.

4. Conclusion

In this article, we have a look at the concept of directories that are in Linux. Besides that, we get familiar with what the directories structure looks like in Linux.
Above all, we presented you with some examples of how to delete directories. Deleting directories using commands: rmdir, rm and find. Using rm with find you can delete directories based on different criteria efficiently and fast.
To sum up, using each of the commands presented above is a simple and easy process and must be used with caution.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area


Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments