How to Remove a Directory in Linux

The command to remove a directory (folder) on Linux is:

rm -r /path/to/directory/

It will delete it no matter if the directory is empty, or not empty.

-r is the option that tells the rm (remove) command to do this recursively. That is, remove the directory itself, along with everything else it might contain inside.

Easy enough, but there are a few extra things we should be aware of.

Let's start with the basics.

Remove a Directory on Linux by Specifying the Full Path to It

A full path to a directory is like the "full address" to it.

Imagine this.

  • There's a building called B19.
    • In this building, an apartment called A21.
      • Inside this apartment, a bedroom that we want to refer to.

Under Linux, the full path to that room (the bedroom) would be:

/B19/A21/bedroom/

Everything in Linux starts from the root directory, symbolized by /. This is the top-level directory, which contains everything else.

Now let's imagine this:

  • In / we have a subdirectory.
    • This subdirectory is called home.
      • Under home we also have another sub-subdirectory called alex.
        • And, finally, in alex we have yet another subdirectory called old_project. Which is the directory we want to remove.

The full path to this directory is:

/home/alex/old_project/

So, to remove this directory, using the full path to it, we'd run this command:

rm -r /home/alex/old_project/

But notice this doesn't output anything. Even though the directory was successfully removed.

Add the -v Verbose Option to the rm Command to See What It Removes

To see more details, we can add the -v (verbose) option into the mix. Think of verbose as meaning "detailed".

So if we combine our options, -r for recursive, and -v for verbose, we end up with this command, to remove a directory and print out details about what is being removed:

rm -rv /home/alex/old_project/

Now the output lets us see what is going on:

Remove a Directory on Linux by Specifying the Relative Path to It

We can also use a relative path in our rm command. This means that we can tell rm what to delete, relative to where we currently are. Here's an example.

Let's say the directories we want to remove are inside another directory, /home/alex/projects/. First, we can switch to that directory:

cd /home/alex/projects/

cd is short for change directory.

Essentially, we have "moved inside" /home/alex/projects.

Now we can use the ls (list) command to see what we have inside our current directory (sometimes also called the working directory).

ls

We can see we have two directories in here, called project1 and project2. Let's say we want to delete the project1 directory. Instead of a long command (with a full path) like:

rm -rv /home/alex/projects/project1/

We can run the much shorter rm command with a relative path to project1:

rm -rv project1/

Since project1 is inside our current / working directory, we don't need to specify the full path to it. Our operating system will know we are referring to something that is "here", in the current location (current directory).

There are more ways to specify relative paths, but this is the simplest example. If you want to learn more about full paths, relative paths, and other commands to work with files and directories, see this blog post: Linux - Create, Delete, Copy, and Move Files and Directories.

Now let's see other ways to remove directories on Linux, along with the solutions to some problems we might encounter.

Frequently Asked Questions (FAQ)

How to Remove an Empty Directory on Linux?

Sometimes, we might want to remove a directory only if it's empty. As a way to clean up directories which have no use, and no contents inside.

To remove an empty directory on Linux, we can use the rmdir command. For example, to remove an empty directory called empty_dir, using a relative path, we can run:

rmdir -v empty_dir/

And with the full path, the command to delete an empty directory could be something like this:

rmdir -v /home/alex/empty_dir/

Just like rm, rmdir also supports the -v verbose option. So it's useful to add it in there, to get some useful output from the command.

The nice side-effect of rmdir is that it protects us against some possible accidents. Imagine we want to remove an empty directory. But we type the wrong name, for some reason, and point rmdir to another directory full of files.

Well, no problem. If rmdir notices the directory is not empty, it will throw an error.

And just like that, we avoid accidentally removing a useful directory that has stuff inside.

How to Remove Directories with Write-Protected Files Inside

There is an annoying scenario we might encounter from time to time. Where we run a command like this:

rm -rv projects/

And the result is that rm asks for confirmation to delete every write-protected file, or directory inside. Which might look like this:

Sure, we can type y, and press Enter to confirm the removal of this write-protected file.

But what if it has thousands of such files? It would ask the same question, again, and again, until we end up with a wall of text, and never-ending sea of confirmations:

This usually happens for files or directories that lack the +w write permission on them. Although, it can also happen for other reasons, as we'll soon see. Either way, if we have thousands of such objects, we'd be here for a long time typing y for each entry.

So if we encounter this situation, we have an easier solution. First, we press CTRL+C to exit from this never-ending confirmation nightmare:

And then we re-enter the same command, but we add the -f force option to rm. In our case, the previous rm -rv projects/ would become:

rm -rvf projects/

And just like that, problem fixed.

Everything will be deleted immediately, without requiring confirmation.

Solve "Permission Denied" Error when Using the rm Command

Now here's something that might look similar to the previous situation, at first glance. We run a command like this:

rm -rv /var/www/wordpress/

And we get the same "write-protected" message we saw earlier.

However, this time, after we type y, we get an error:

This happens because our current user (the one we're logged in as) does not have the required permissions to modify, or delete this directory.

So, how do we get the required permissions? Well, if our user has "sudo privileges", we can just add sudo in front of our command.

A command like:

rm -rv /var/www/wordpress/

Becomes:

sudo rm -rv /var/www/wordpress/

We might be asked for our current user's password. And then the directory will be removed.

What sudo does is that it runs the command after it with "root" (super-user) privileges. "root" is the user that has permissions to do almost anything on a Linux system.

If the user we're logged in as does NOT have "sudo privileges", we can go another route. And simply log in as the "root" user, temporarily, with the su substitute user command:

su --login

In this case, we shouldn't type the password of our current user. But, rather, the password for the root user. Once that's typed correctly, and we press enter, we'll be logged in as the "root" administrative user.

At this point, we can re-run our command:

rm -rv /var/www/wordpress/

And the directory will be removed.

It's a good idea to also log out from the "root" user, once those privileges are no longer needed. We can log out with a simple logout command:

logout

This should return us to our previous user:

Conclusion

And that's about it. Any scenario you encountered that we didn't explain here? Please add a comment below if that's the case.