How To Force Delete Directory In Linux?

The rmdir command is used to delete directories in Linux. The rmdir command simply removes or deletes an empty directory but does not work with non-empty directories. If we try to remove a non-empty directory in Linux with the rmdir command we get an error like below.

Force Delete Directory with rm Command

We should use the rmdir command in order to force delete the directory. Force means if there is a minor error for the deletion the removal is not stopped. Forcing to delete a directory also completes the delete operation even if the directory has files and folders. The -f option is used to specify the force option. The -r option is used to delete a directory recursively event there are child directories inside the target directory. We can simply express the -r and -f options like -rf .

$ rm -rf nmap/

We can delete a directory that is not located in the current working directory. We should provide the target directory absolute or relative path to the rm command. In the following example, we delete the tmp directory which is located under the /var .

$ rm -rf /var/tmp

Force Delete Multiple Directories with rm Command

We can force to delete multiple directories by using the rm command. The directory names are provided to the rm command as parameters. In the following example, we delete the directories nmap/ , tmp/ , db/ .

$ rm -rf nmap/ tmp/ db/

Verbose and Force Delete Directory

By default deleting directories forcibly does not display verbose or detailed information about the directory removals. We can use the -v option in order to delete the directory forcibly and also display information about the deleted directory.

$ rm -rfv nmap/

Leave a Comment