How to Delete a Directory in Linux

Linux Terminal Feat

There are a couple of ways to go about deleting a directory in Linux. You can either handle it graphically from your desktop environment or do it directly from the command line. Either way will work, and they’re both just as effective.

Graphical Method to Delete a Directory in Linux

Every desktop environment (and file manager) is slightly different. This article will walk through the steps of using Budgie with Nemo as a manager, but the process is roughly the same with every desktop.

Deletefolder Trash

Open your file manager and browse to the location of the directory you want to delete. Once you’ve found it, right click on that directory to open the menu of available actions. Depending on your environment, you may see both “Delete” and “Move to Trash” or just “Move to Trash.” It’s always a safer bet to move the directory to the trash, as you can recover it if you make a mistake. Click “Move to Trash” on your menu.

The file manager will sometimes ask you if you really want to delete the folder. Confirm that you do. The directory will move from its current location and will appear in the trash folder instead. There’s only one step left to completely zap it from existence!

Deletefolder Emptytrash

If you’re absolutely sure you don’t need the directory you just removed, open the trash. You’ll see the folder there, and you can even browse its contents.

If there’s nothing there that you need, you can either right click on the folder itself to delete it permanently or click in the white space of the directory to bring up a menu with the option to empty the trash. In Nemo, there’s a convenient “Empty Trash” button (depicted) near the top-right corner of the window. Do whichever you prefer. Remember, there’s no way to undo this. The directory isn’t coming back.

Also read: Using du to Free Up Disk Space in Linux

Using the Command Line to Delete a Directory in Linux

There’s an even more direct way to remove a directory from the command line. The rm command can be used to remove both files and directories.

This is useful as a method that will give you more control over the file removal system or as a means to delete something when your file manager is inaccessible.

To fully explain what this command can do, we need to open a terminal and navigate somewhere safe to test various commands. The Documents directory inside your home folder should do just fine.

cd ~/Documents
Deletefolder Terminalcd

The ~ is a symbol that represents your home directory.

Let’s start by creating a folder using mkdir Test. You can try to delete this short-lived folder using rm Test.

Deletefolder Removefailure

That didn’t quite work, did it?

Although rm‘s manual describes the command’s function as “remove files or directories,” there’s a little caveat: the command by itself will not remove directories.

To erase a directory, you’ll have to run the command in recursive mode using the -r flag. Directories are often homes for other little files, making this flag necessary. Before you get elated by this little discovery, it’s important to note that some Linux distros will prompt you before the directory is deleted.

Since we’re sure we want to get rid of the Test directory, we need to use the -f flag, which forces the removal to go through without prompting you. The final command should look like this:

rm -rf Test
Deletefolder Success

Try this again, but this time, put a few files into Test. You can do this easily with your file manager, but since you’re already in the terminal, why not have a little fun doing it with a few other commands that may be useful at a later time?

mkdir Test
cd Test
touch file1.txt file2.txt file3.txt file4.txt
cd ..
rm -rf Test
Deletefolder Advanced

It looks like a lot to take in, but what you did is simple to explain. First, you created a directory called “Test.” You then ordered your shell to move to that directory and create four files. The .. after cd ordered your shell to move back up one directory to the parent of Test (i.e., back to your documents folder).

The final command you typed did a recursive forced deletion of Test and all the files inside it. This is why the -r flag is necessary when removing directories.

There’s More to It than Meets the Eye

For the most part, the -r and -f flags in rm are pretty much all you’re going to use when typing the command in your terminal. The following are are some other useful flags:

-iPrompts you for each file deletion. It’s useful when doing a recursive removal of a folder containing files you may or may not remember being important. When you want to confirm the deletion, type y when prompted. Type n if you want to keep that file. Keeping a file during a recursive removal may stop the removal process. Example:

rm -ri /home/user/Database.
Deletefolder Interactive

-dRemoves a directory only if it’s empty. This is an essential flag when you want to clear up folders that you never bothered to fill. It comes especially handy when you want to write a script that deletes all empty folders in a particular area of your drive. If you’re just issuing commands in the terminal, you won’t find much use for this flag. Example:

rm -d /home/user/Database/UID01
Deletefolder Emptyflag

-vRuns rm in verbose mode. This flag helps you troubleshoot issues by looking at what rm tells you each step of the way while trying to execute your instructions. If something is not quite right, this is what you use to walk through the process. Example:

rm -rv /home/user/Database
Deletefolder Verbose

--version – Tells you which version of rm you’re running. You’ll probably never need to use this flag, but it’s there in case you’re wondering if you’re on the latest version.

Deletefolder Version

Just One More Command

No article about removing files in Linux is complete without talking about rmdir. It’s essentially a clone of rm -d, with its own uniquely useful flag. Since rmdir can only remove empty directories, most users don’t pay much attention to it. However, it is highly useful in scripts when you want to quickly flip through a large quantity of directories and remove everything that’s empty as quickly as possible.

The only flag that’s really noteworthy in rmdir is -p, which removes all parent directories of an empty directory if they are also empty. Scripts can use this to go to the top of every file hierarchy and quickly snipe out all empty parents of empty folders to do cleanup tasks. The rm -d flag is limited in this case, as it only removes a directory if it’s empty at the moment but doesn’t scan the parent directory after deleting its child.

Frequently Asked Questions

1. What do I do if I get “permission denied”?

If you are having trouble with permissions when deleting something, first double-check that you’re not trying to delete something essential to your system or applications you use. Once you’re sure you’re just deleting some unessential file or directory, go to your terminal and type sudo before your rm command. This should bypass any permission restrictions.

Don’t feel like using the command line? Most file managers installed with Linux distros should be able to help you here. Right-click any empty space in the file view, and you should see an option saying “Open as root” or “Open as Administrator”. Click on that, and enter your password when prompted, a new window should open with elevated privileges, allowing you to do whatever you want to your heart’s content.

2. Is rm safe?

Yes and no. The rm command is only a deletion command. It doesn’t question whether the files and directories you are deleting is sensitive or not. As long as you’re certain you’re not deleting something that shouldn’t be deleted, rm is safe.

Generally, you won’t cause problems to the operating system if you stick within the bounds of the “/home” directory. Once you venture beyond that, you are no longer in calm seas and should really watch where you sail.

3. Can I use rm or rmdir in other operating systems?

This depends entirely on which operating system we’re talking about. The rm command for macOS is strikingly similar to Linux’s, which isn’t surprising considering that both wear the old gown that once was UNIX.

With Windows, things are a little different. There’s no single command for removing files and directories, instead the set of operations is split into two distinct groups. The Windows command line recognizes rd to delete directories and del to delete single files. Flags for rd as found in its manual page show some similarities to Linux’s rm -rf.

Conclusion

With all the information provided here, you will hopefully come out of reading this with a level of confidence in using your terminal and file manager to clear your drive of clutter. Just don’t forget to tread carefully and take each step with some level of respect for the power you wield. There’s no need to hurry. Your terminal and file manager aren’t going anywhere!

Read on to learn how to install Linux on Windows with WSL and reset the root password in Linux.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Miguel Leiva-Gomez
Miguel Leiva-Gomez - Staff Writer

Miguel has been a business growth and technology expert for more than a decade and has written software for even longer. From his little castle in Romania, he presents cold and analytical perspectives to things that affect the tech world.