How To Delete Empty, Nonempty Directory In Linux

Linux file system consist of directories. A system administrator generally work with directories and related operations. In this tutorial we will look how to delete empty or nonempty linux directories.

Delete or Remove Empty Directory

We can remove or delete empty directories with rmdir command. In this example we will remove the empty directory named stuff .

$ rmdir stuff

Delete Or Remove Empty and Multilevel Directory

If our empty directories are in hierarchical way we need to provide the -p option like below. In this example we will use -p and this will remote both stuff/test and stuff directories.

$ rmdir -p stuff/test/

Delete or Remove Non-Empty Directories Recursively

rmdir is used to remove and delete empty directories but what if the directories are not empty. In this situation we will use rm command with some extra parameters. -r option is used to remove files and directories recursively. In this example we will remove rar directory and its child file and directories recursively.

$ rm -r rar

Delete or Remote Non-Empty Directories Forcibly

Some times some of the files and directories may have some problematic options. This situation may prevent deletion operation. We can use -f option in order to remove forcibly way.

$ rm -rf rar

Remove with Sudo

If the files and folders we want to delete are owned by root or other user and we do not have required permissions we need to use sudo . sudo will made the rm command run as root user which has highest privileges.

$ sudo rm -r rar

Print Delete Operation Details with Verbose

While deleting the files and folders we may need detailed information about removal. We can save the delete logs. We should provide -v option which creates verbose output.

$ rm -r -v rar
Print Delete Operation Details with Verbose
Print Delete Operation Details with Verbose

LEARN MORE  How To Get The Size Of Directory In Linux From Command Line?

Leave a Comment