0

How To Remove a Directory in Linux

Posted by Arch on June 27, 2019
How to Remove a Directory in Linux

Most people only know of Linux as a curiosity, and they bring it up as an afterthought when discussing Windows or iOS. But to a specialized minority, Linux is an important tool.

As you may already know, operations in Linux can be more complex than in other operating systems. To perform even a simple operation such as removing a directory requires a series of commands. In this article, we’ll go over how to do that and touch on some helpful additional tips. If you’re new to Linux, this should help you gain some perspective.

Removing an Empty Directory

In Linux, the word ‘directory’ refers to a location where data is stored in a file system. Think of it as an analog of the folder in Windows systems.

But removing a directory in Linux is not quite as easy as clicking delete, and there are a few things you should consider first.

linux

If you have a directory that is empty, a command you can use is rmdir. First, launch the terminal app on your machine. Then, enter the following syntax:

rmdir DirectoryName

Enter this command, and replace “DirectoryName” with the name of your directory. Remember that this will only work on directories that are empty. If you attempt to use it on a directory that isn’t empty, it will return the output “Directory not empty.” Now, that doesn’t mean it can’t be removed, it just means you’ll need a different command.

Removing Directories with Content in Their Subdirectories

 If you’re dealing with a directory that contains other files, the process will be only slightly different. Instead of the rmdir command, you can use rm. This is fundamentally the same command, but not specific to directories, and the addition of -r will make it recursive. That is to say, it will hierarchically remove the folders in the directory until it is empty and then remove the directory.  So your new syntax will read:

rm -r DirectoryName

As in the previous example, replace DirectoryName with the name of your actual directory. One caveat is that you will receive a prompt when deleting every file. You can bypass the prompts by using -rf instead of -r, but this isn’t considered best practice.

Removing a Directory You Don’t Own

 While not recommended, sometimes you will need to remove directories that you don’t have permission to remove. If you attempt to remove one of these, you will be denied access. However, if you’re absolutely sure that you’re removing directories that you don’t need, you can use the sudo command. So, your final syntax to remove a directory you don’t own (while avoiding any prompts telling you about it), should look like this:

sudo rm -rf DirectoryName

It’s not a recommended course of action but don’t be afraid to use it if you’re certain you need it.

Some Clarification on Commands

 The letters you’re using are telling the OS to do very specific things. Here’s a breakdown of what happens when you use these and some other useful commands.

How to Remove Directory in Linux

-r  — Removes a directory recursively, hierarchically removing files rooted in it.

-f — When removing files, it does not allow permission prompts irrespective of the file status.

-i — Creates a prompt on every file deletion, useful when you’re dealing with a few sensitive files.

-v — This shell command will generate a diagnostic message for every directory that is processed as a part of rm.

Handle With Care

 That should be enough to introduce you to the rm command. Keep in mind that you are permanently removing directories, and you should always be quite sure that you want them gone. Especially when using the -r and -rf commands, you can easily lose data that you may have wanted to keep. Keeping that in mind, go and get rid of some directories.

What other commands would you like to see elaborated? If you were taking an introductory Linux course, what topics would you like to see covered?

Leave a Reply

Your email address will not be published. Required fields are marked *


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