Home | About | Categories | All Tips & Tutorials

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

ID: 309

Category: Linux Terminal

Added: 31st of May 2022

Views: 1,625

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 a directory that is not empty, 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.