How to Remove a Non-empty Directory in Linux

By Dhiraj Bezbaruah | Jan. 11, 2022, 4:36 p.m.

To delete a file in Linux, we use the below command-

$ rm <file_name>


$ rm test.txt

Now to delete a directory we can use the below command, provided the directory is empty. If the directory has any files or sub-directories then it will show an error-

$ rmdir <dir_name>

To delete a non-empty directory you need to use the below command

$ rm -r <dir_name>

The above command will recursively delete all files inside the directory.

If a directory has protected files inside then we can forcefully delete them along with the directory using the below command

$ rm -rf <dir_name>

Hope this helps. Please comment down if you have any questions regarding deleting files and directories in Linux.

Share this article