Ubuntu: Remove directory from Terminal or File Manager
Are you trying to delete a directory on your Linux PC? Not sure how to go about it? We can help! Follow along with this guide as we show you how to remove directories on Linux!
Ubuntu remove directory – rm command
The best way to remove a directory on the Linux platform is via the terminal using the rm (remove) command. The remove command is very versatile and easy to use, even if you’re new to Linux and not very familiar with the command-line.
To remove a directory, you will need to open up a terminal window. To open up a terminal window, press Ctrl + Alt + T on the keyboard, and it should launch. Or, search for “Terminal” in the app menu.
Once the terminal window is open, execute the ls command to view the home directory’s contents. The home directory is the default folder in which the terminal starts sessions.
ls
Look through the home directory for the folder you wish to remove. If your folder isn’t in the home folder, try to find the exact location. In this example, the folder we’ll delete with the rm command is located inside the “Downloads” folder named “Misc.” Yours will differ.
In this example, the folder we wish to delete is inside of the “Downloads” folder. We must enter the “Downloads” folder from inside the home directory by using the CD command.
cd /home/derrik/Downloads
Once inside the “Downloads” folder, we can delete the “Misc” folder using the rm command below.
rm -r Misc/
Get warnings before deleting
Do you wish to be warned before attempting to delete a folder on your Linux PC by making use of the rm command? Thanks to the i command-line switch, you can with this example:
rm -ri /home/USERNAME/LOCATION/OF/FOLDER/
After entering the rm -ri command in a terminal prompt, the rm tool will ask “remove directory”? Press the “Y” key on the keyboard to confirm that you indeed do want to remove the folder, and then press Enter to continue.
Forced deletion
Sometimes you will have folders on your Linux PC that refuse to be deleted, even after running the rm -r command. You will need to execute the rm -r with the -f command-line switch to get around this.
The -f command-line switch tells the rm command to remove a directory and ignore all warnings forcibly. Here’s how to use it.
WARNING: Never use the rm -rf command on system directories on your Linux PC, especially if you are new to Linux! You could permanently break your system and corrupt your installation!
rm -rf /home/USERNAME/LOCATION/OF/FOLDER/
After executing the rm -rf command in a terminal window, the stubborn folder that you couldn’t delete will be gone!
More info
There’s a lot of command-line options for the rm command on Linux. In this guide, we’ve gone over the basics. However, if you require more information on how the program works, take a look at the RM manual by executing the command below.
man rm
Ubuntu remove directory – rmdir command
The rm command is very useful for deleting folders filled with items. However, if you have an empty directory you wish to remove, the best way to do that is with the rmdir command. Here’s how to use it.
First, open up a terminal window. You can open up a terminal window on the Ubuntu desktop by pressing Ctrl + Alt + T on the keyboard or searching for “Terminal” in the app menu.
Once the terminal window is open, execute the ls command to view the directory’s contents the terminal is in. In this example, we’ll be working in the home folder.
ls
After running the ls command, find the empty folder in the ls readout. Then, execute the rmdir command to delete the empty folder.
rmdir MY_EMPTY_FOLDER/
Ubuntu remove directory – Ubuntu file manager
If the command-line doesn’t work for you, it is possible to remove a directory on Ubuntu using the Ubuntu file manager. Deleting folders with the Ubuntu file manager is very straightforward and highly recommended if you’re not command-line savvy. Here’s how it works.
To start, open up the Ubuntu file manager by clicking on the dock’s file manager icon. Or by pressing Win and searching for “Files” in the Activities search box.
Once the Ubuntu file manager is open, use it to locate the directory you wish to remove. Then, right-click on the folder with the mouse.
Upon right-clicking, locate the “Move to trash” button to delete it. Empty the “Trash” folder in Ubuntu to make the removal permanent.