How to Remove Empty and Non-Empty Directories in Linux [Snippets]
Let's look at how to remove a non empty directory in Linux
Join the DZone community and get the full member experience.
Join For FreeRemoving a directory in Linux is harder than you think, and it's not uncommon to get errors that look like this:
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.
rm -r folder
Remove Multiple Non-Empty Directories in Linux
Simply separate the folder names with a space, and use rm -r
:
rm -r folder1 folder2 folder3
Remove an Empty Directory
If your directory is empty, with no folders or files in it, then use rmdir
:
rmdir folder
Published at DZone with permission of Johnny Simpson, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments