Category: Linux Terminal

Added: 31st of May 2022

Viewed: 247 times

Using the rm command to delete a directory, files using the terminal in Linux

Basic usage of using the rm command to delete directories and files using the terminal

To delete an empty directory use the following command

rm -d {folder_name}


If you try and delete an empty directory using the -d argument you will get the following error
rm: cannot remove 'folder': Directory not empty


To delete a directory including all files use the -r switch
rm -r {folder_name}


To prompt before every removal use the -i switch
rm -ri {folder_name}


To explain what is going on after every removal using the -v switch
rm -riv {folder_name}


If you have a directory and files owned by root and you issue the rm -r command in the terminal you will get the following error
rm: cannot remove 'folder/file': Permission denied


To remove the directory and files you will issue the following command. You will need to enter your root password
sudo rm -r {folder_name}


When you use the rm command to remove directories and files they do not get sent to the trash

Why do I need to use the terminal?
If your using a desktop environment you can just as easily right click on the directory and send the directory and it's contents to trash, however if a directory and it's files are owned by root then you would need to issue the rm command through the terminal as some Linux distributions do not allow to open a directory as root via the right click menu.

Another reason you would need to use the rm command is if you needed to delete a directory and it's files on a remote server.

Other Tips & Tutorials

Linux Terminal

The whatis command in Linux

Posted: 25th of July 2022

Views: 149

Linux Terminal

Export man pages in .html or .pdf format by installing mandoc on Ubuntu and Ubuntu based distributions

Posted: 9th of July 2022

Views: 260

Linux Terminal

Using the rm command to delete a directory, files using the terminal in Linux

Posted: 31st of May 2022

Views: 247

Linux Terminal

Check amount of free disk space using the df command in the terminal on Linux

Posted: 23rd of May 2022

Views: 265

Linux Terminal

How to access linux man pages and output specific manual page to a .txt file

Posted: 29th of November 2021

Views: 887

Linux Terminal

Kill multiple processes using the pkill command in Linux

Posted: 5th of January 2021

Views: 1581

Linux Terminal

Display free memory using the terminal in Linux

Posted: 17th of December 2020

Views: 1477

Linux Terminal

Using mlocate to find files by name, extension using the terminal in Linux

Posted: 27th of November 2020

Views: 1377

Linux Terminal

Capture screenshots of websites using cutycapt in different formats using the terminal in Ubuntu and Ubuntu based distributions

Posted: 15th of November 2020

Views: 1771

Linux Terminal

Display a calendar in the terminal

Posted: 6th of November 2020

Views: 1628