How to Remove Empty and Non Empty Directories in Linux

Removing a directory in Linux is harder than you think, and it's not uncommon to get errors that looks like this:

shell Copy
folder: Directory not empty folder: is a directory

The confusion comes because rmdir cannot be used to delete a directory with files or folders inside of it.

Remove a non-empty directory in Linux

To remove an unempty directory in linux, pass the -r flag to rm. -r means recursive, so it deletes everything in a folder, including the folder itself. Replace 'folder' below with your folder's name or location.

shell Copy
rm -r folder

Remove multiple non-empty directories in Linux

Simply separate the folder names with a space, and use rm -r:

shell Copy
rm -r folder1 folder2 folder3

Remove an empty directory

If your directory is empty, with no folders or files in it, then use rmdir:

shell Copy
rmdir folder
Last Updated Tuesday, 20 April 2021
Click to Subscribe Subscribed


Subscribe to stay up to date with our latest posts via email. You can opt out at any time.

Not a valid email