How to remove a directory in Linux

In this tutorial, we are going to explain how to remove a directory in Linux OS.

Removing directories in Linux is simple but important as it helps free up disk space on your server. System administrators, developers, and other Linux users remove directories according to their everyday needs. The removal can be done manually with a command on the command line, through the GUI of the control panel, or automatically through a script.

Below, we will show you how to remove the directory with real examples. Let’s get started!

Prerequisites

Update the system

We will use the latest Ubuntu 24.04 OS. We assume that you have a fresh installation of Ubuntu 24.04 and before doing anything on your server you should update the system packages to their latest versions available:

sudo apt update -y && sudo apt upgrade -y

Remove directory with rmdir command

The rmdir command can be used in special cases only when the directory is empty. If the directory has some content the rmdir command will not work and the console will throw an error. The syntax of the rmdir is the following:

rmdir [option] directory_name

Let’s try to remove a non-empty directory with files and folders:

rmdir wordpress/

Since this directory contains all WordPress files, we will receive the following error message:

root@host:~# rmdir wordpress/
rmdir: failed to remove 'wordpress/': Directory not empty

If we execute the command on the empty directory it will remove it without any issue:

root@host:~# rmdir emptyDir/
root@host:~#

In addition to this, this is the list of the options that the rmdir command takes as an argument:

rmdir -p #Remove an empty subdirectory and its parent directory.

rmdir -v #Print the information that the specified directory was deleted.

If you want to know more about the rmdir command you can use the following command:

man rmdir

You should receive the following output:

RMDIR(1)                                                                   User Commands                                                                   RMDIR(1)

NAME
       rmdir - remove empty directories

SYNOPSIS
       rmdir [OPTION]... DIRECTORY...

DESCRIPTION
       Remove the DIRECTORY(ies), if they are empty.

       --ignore-fail-on-non-empty
              ignore each failure to remove a non-empty directory

       -p, --parents
              remove DIRECTORY and its ancestors; e.g., 'rmdir -p a/b' is similar to 'rmdir a/b a'

       -v, --verbose
              output a diagnostic for every directory processed

       --help display this help and exit

       --version
              output version information and exit

Remove directory with rm command

The rm command is used more than the rmdir command since it has more options and can remove empty or non-empty directories recursively. For example, to remove the WordPress directory and its subdirectories and files we can use the following command:

rm -rf wordpress/

After removal, you will receive an empty line because the directory was removed recursively in no time.

root@host:~# rm -rf wordpress/
root@host:~#

If we have an empty directory we need to use the following command:

rm -d emptyDir

As you can see the rm command has options before the directory name and you can check them below: rm -d #Remove an empty directory using the rm command. rm -r #Remove a non-empty directory and its content. rm -rf #Ignore any prompt when deleting a write-protected non-empty folder.

If you want to know more about the rm command you can use the following command:

man rm

You should receive the following output:

RM(1)                                                                      User Commands                                                                      RM(1)

NAME
       rm - remove files or directories

SYNOPSIS
       rm [OPTION]... [FILE]...

DESCRIPTION
       This manual page documents the GNU version of rm.  rm removes each specified file.  By default, it does not remove directories.

       If  the  -I or --interactive=once option is given, and there are more than three files or the -r, -R, or --recursive are given, then rm prompts the user for
       whether to proceed with the entire operation.  If the response is not affirmative, the entire command is aborted.

       Otherwise, if a file is unwritable, standard input is a terminal, and the -f or --force option is not given, or the -i  or  --interactive=always  option  is
       given, rm prompts the user for whether to remove the file.  If the response is not affirmative, the file is skipped.

OPTIONS
       Remove (unlink) the FILE(s).

       -f, --force
              ignore nonexistent files and arguments, never prompt

       -i     prompt before every removal

       -I     prompt  once  before removing more than three files, or when removing recursively; less intrusive than -i, while still giving protection against most
              mistakes

Removing the directory using the control panel GUI is straightforward. You can navigate to the File Manager, select the checkbox next to the directory, and click DELETE from the options menu.

That’s it. You successfully remove a directory in Linux. If you have any difficulties doing this task you can always contact our technical support at LinuxHostSupport. We are available 24/7 and will be happy to help you.

If you liked this post on how to remove a directory in Linux, please share it with your friends or leave a comment below.

Leave a Comment