How to Delete a Directory in Linux

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 you can 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 (or file manager) is slightly different. This article will follow KDE Plasma, but the process is roughly the same with every desktop.

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 because you can recover it if you make a mistake. Click “Move to Trash” on your menu.

The file manager will 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.

The Trash

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 you can click in the white space of the directory to bring up a menu with the option to empty the trash. Do whichever you prefer. Remember, there’s no way to undo this. The directory isn’t coming back.

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.

Open a terminal window and navigate to the location of your directory. When you’re there, run the following command:

rm directory-name

It didn’t work, did it? That’s because rm needs to be given the -r flag to remove a file recursively to delete a directory. On some systems just using the “-r” flag will prompt a message asking if you’re sure you want to delete the directory. To force it and automatically confirm that you do, use -rf. Be forewarned, rm doesn’t move a file to the trash. It simply deletes it entirely. Always double-check when you’re deleting a directory (or files). Really bad things can happen.

rm-rf directory-name

Alternatively, you can also use the command rmdir to delete a directory, but it will only work on an empty directory. So if the folder is full of files, this command will fail. Using rm is a safer bet.

Conclusion

There you have it, two ways to delete directories on a Linux system. Take caution when using either one, and remember, using rm, especially as root, can be extremely dangerous.