DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for How to Remove Empty and Non Empty Directories in Linux
Johnny Simpson
Johnny Simpson

Posted on • Originally published at fjolt.com

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:

folder: Directory not empty
folder: is a directory
Enter fullscreen mode Exit fullscreen mode

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

Remove a non-empty directory on Linux

To remove a directory on linux, which is not empty, 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.

rm -r folder
Enter fullscreen mode Exit fullscreen mode

Remove multiple non-empty directories on Linux

If you want to remove multiple directories on linux, and they are not empty, simply separate the folder names with a space, and use rm -r:

rm -r folder1 folder2 folder3
Enter fullscreen mode Exit fullscreen mode

Remove an empty directory on Linux

If you want to remove a directory on linux, and your directory is empty, with no folders or files in it, then use rmdir:

rmdir folder
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

16 Libraries You Should Know as a React Developer

Being a modern React developer is not about knowing just React itself. To stay competitive, it is highly recommended to explore the whole ecosystem. This article contains some of the most useful React component libraries to speed up your developer workflow.