How to Delete a Full Directory Including Files in Linux

Linux Command Line is a powerful tool that lets you do many things faster and easier than through the GUI. One of its essential capabilities is to create and delete files and folders, though we’ll stick to deleting folders in this article.

How to Delete a Full Directory Including Files in Linux

Keep reading to learn how to use the “rm” and “rmdir” commands to get rid of folders, sub-folders, and files you no longer need.

Use “rm” to Delete Directories

There are many commands you can use to delete a directory. The choice should depend on what you want to do and how you want it done. Linux Command Line is super-flexible in this regard, maybe even more so than its Windows and Mac counterparts.

It is worth noting that Linux doesn’t make a distinction between folders and files like Mac and Windows operating systems do. Instead, it treats folders as file groups. In this section, we’ll examine the rm command. Let’s get started.

Use “rm” to Delete Directories

rm –d nameofthedirectory

The command above will only let you delete a single, empty directory. This is the most basic command for removing/deleting folders.

rm –d nameofthedirectory1 nameofthedirectory2

The command presented above will delete multiple folders. The catch here is, like with the previous one, they all have to be empty. If it happens that the first folder you named isn’t empty, Command Line will not try to delete other folders. It will just stop, without giving you an error message.

rm –r nameofthedirectory1 nameofthedirectory2

The command above will delete all the specified folders, their sub-folders, and files in them. This is possible thanks to the “-r” option which replaces the “-d” from the previous command. In Linux Command Line, “-r” stands for recursive. It can be used on its own and combined with other options.

rm –rf nameofthedirectory

When you execute an “rm –r” command, Linux Command Line will ask you for permission to delete any sub-folders and files that are write-protected. However, if you type in “rm –rf” instead, you will not be prompted. The letter “f” stands for “force.”

You should be careful when deleting folders and files with an “rm –rf” command, as you might lose important data or damage the operating system. System files and folders can be deleted more easily on a Linux system than on Windows or Mac.

sudo apt-get install tree

To get a better understanding of what you’re about to delete, you should install the tree package through the apt-get utility. This works for Ubuntu and the rest of the Debian family. If you’re on another distribution, use its own package management tool. When you execute the above command, Command Line will display the folder and file structure of the folder you’re in. This way, you can easily check whether there are any files or sub-folders that should remain intact.

tree path/to/your/directory

The command presented above will allow you to view the structure of another folder in your Linux system.

Advanced Commands

There are other variations of the “rm” command, such as “–no-preserve-root,” “–preserve-root,” “–one-file-system,” and others. However, they’re intended for experienced Command Line users. If you make a mistake with one of these, you might delete a portion or even all system files on your computer. Due to their complicated nature, we’ll save them for another Command Line tutorial.

Use rmdir to Delete Directories

You can also use the rmdir set of commands to delete folders. However, the rmdir commands can only take care of empty folders and can’t delete files contained inside the folders marked for deletion. There are several useful rmdir commands, and we’ll take a look at them in this section.

Use rmdir to Delete Directories

However, you can trick the Command Line to delete a non-empty folder with the parent option, though more on that a bit later.

rmdir nameofthedirectory

This is the most basic “rmdir” command out there. It will delete an empty directory that’s within the directory in your current location. For example, if your current location is desktop and you have an empty “New Folder” in it, this “rmdir” command will take care of it.

rmdir nameofthedirectory1 nameofthedirectory2

In case you have multiple folders you’d like to delete, you can use the above variation of the “rmdir” command. All specified folders (directories) will be deleted, but they have to be contained within the directory you are currently in. To delete directories elsewhere, refer to the next command.

rmdir /path/to/your/directory

Linux Command Line allows you to delete any directory from your current location, wherever it may be. To do that, you’ll have to enter the full path towards the directory or directories you’d like to get rid of.

In case you’ve attempted to delete a folder that contains sub-folders and/or files, Command Line will show you an error message stating: Directory not empty. Needless to say, it will not delete the specified folder.

In case you specified three folders and the first one proved to be not empty, Command Line will stop processing your command as soon as it runs into the first folder. You will get the same error message as in the previous case and the Command Line won’t attempt to delete other folders on the list.

You can remedy this by adding the following option: –ignore-fail-on-non-empty. This will force the Command Line to continue executing the command even if it encounters non-empty folders. The command might look something like this: rmdir –ignore-fail-in-non-empty NewFolder1 NewFolder2 NewFolder3.

rmdir –p nameofthedirectory1 nameofthedirectory2

The above command might help you trick Linux into deleting a non-empty folder. It uses the “-p” option, also known as the “parent” option. Here’s how it works.

Let’s say that you have a folder named Pics and a folder named ColorPics inside it. Let’s assume the latter is empty and is the only item within the Pics folder. When you execute the command “rmdir –p ColorPics Pics,” the Command Line will delete the ColorPics folder because there’s nothing in it. After that, it will check the status of the Pics folder, determine it too is empty, and delete it.

Know the Power of Command Line

Command Line allows you to do many things on a Linux system. Be careful if you don’t have much experience, as it is easier to damage the system on Linux than on Windows and Mac.

Have you used Command Line to delete folders and files before? Which commands did you use? If you think we’ve missed some good options, let us know in the comments section below.

Disclaimer: Some pages on this site may include an affiliate link. This does not effect our editorial in any way.