How To Remove or Delete A Directory In Linux?

Linux provides different commands in order to remove or delete directories and files. But in some cases, this may not work as expected and we can get a message like rmdir: 'dir' Directory no empty which simply means when we try to delete a directory with rmdir command it is not completed. In this tutorial, we will learn how to remove and delete an empty or full directory.

Remove with rm -Rf Command

The most used command to delete a directory with its content is rm command. But rm command is not enough without options -Rf. This will force the deletion of given directories in a recursive manner. In this example, we will delete the directory named backup

$ rm -Rf backup

Remove with Verbose Information

Deleting directory with its children will not provide verbose information by default. But we can print detailed information about the operation with the verbose option -v like below.

$ rm -Rf -v backup
Remove with Verbose Information

Confirm Before Removin/Deleting Directory

If we will delete directories those contain some sensitive ones we may need to confirm before deleting them. We can use -i option which will ask for a removal for confirmation. In this example, we will delete the directory named poftut1 which is not empty. For each directory and files inside poftut1 the remove directory FILE or DIRECTORY NAME will be asked to use. In order to approve, we can enter y or yes to deny we can use empty answer which is just enter or n or no.

$ rm -r -i poftut1
Confirm Before Deleting Directory

Remove/Delete Multiple Directories At Once

We may want to remove multiple directories with a single rm command. We do not have to run rm command multiple times for different directory names. We can provide all directories we want to remove in a single rm command. We can also provide the path of the directory like /mnt/test etc.

$ rm -rf -v Templates poftut1/Case poftut1/Database /home/ismail/SocialFish/
Remove Multiple Directories At Once

Remove/Delete with Root Privileges

In order to delete a file or directory, we need privileges. If the directory is owned by the root user and we are not we can not delete the directory. So we need to get root privileges with sudo command like below.

$ sudo rm -Rf backup

Remove with Python

We can also use some python script or commands in order to delete a directory please look following example where we will use os module remove() function.

LEARN MORE  Linux Directory and File Operations

How To Delete and Remove File and Directory with Python?

import os 


Remove/Delete Directory with find Command

find command is a command used to search and find different types of files and directories the specified path. Find command provides different useful features like regular expression search, extension search or file attribute search, etc. We can also use the find command in order to delete or remove specified directories. We will use the -exec feature of the find command which will run given command for the matched files and folders. We will match the directory type with the -type d option and provide the name or name pattern with the -name '*cache' .

$ find /var -type d -name '*cache*' -exec rm -r {} +

We can also use the -delete option of the find command which will automatically delete matched files and folders. In the following example we will directly delete the

$ find /home/ismail -type d -delete

Remove/Delete Directory with File Manager

Linux and Linux Desktop Enviroments provides different file managers which is used for different operations like add, remove, change files and directories. We can also use file manager in order to remove or delete directory. We will just right-click t the folder or directory we want to delete and find the item like Move to Trash , Delete , Remove , Send to Trash etc. and click.

Remove/Delete Directory with File Manager

Leave a Comment