How To Force Remove Directory In Linux?

Linux provides the rmdir command in order to delete a directory. But while using the rmdir command we may get an error like “rmdir: failed to remove ‘nmap’: Directory not empty”. This is caused by a non-empty directory that contains child directories and files. But we can remove this directory by forcing removal with different methods. In this tutorial, we examine how to force remove a directory in Linux.

Force Remove Directory

The rmdir command is created to remove empty directories and can not be used for directories with content. This content can be another directory or file. This means the rmdir is the safe way to delete empty directories but in daily operations, we generally work with directories with some content. The rm command can be used to force remove the directory with some parameters. The -r option is used to remove recursively all of the child directories and files. The -f option is used to force the remove operation. We should use the -r and -f options like -rf to force remove directory.

$ rm -rf /home/ismail/data

Force Remove Directory Verbosely

By default, the removing directory by force does not create any ouıtput unless there is an error. All directories and files are removed silently. If we want to print output about every delete directory we can use the -v verbose option.

$ rm -rf -v /home/ismail/data

Force Remove Directory Using sudo

Linux uses directory ownership to manage permissions to access directories. If a directory is owned by another or privileged user we can not delete it. We can force to delete the directory by using root privileges with the sudo command. The sudo command simply gets root privileges to delete a directory.

$ sudo rm -rf -v /home/ismail/data

Force Remove Directory with Specific Names

We can also force the removal of directories with specific names. In the following example, we delete directories whose names start with “tmp”.

$ rm -rf -v /home/ismail/tmp*
See also  Delete Non-Empty Directory In Linux - "rmdir directory not empty" Solution

Leave a Comment