Different ways to delete/remove folder/directory in Linux (Ubuntu, Kali Linux etc.) (Empty and non-empty folders)

Last Updated on

rmdir

Used for removing empty folders only. If the folder is not empty, error will be returned.

rmdir folderName
rmdir
rmdir

rm

Used for removing empty and non-empty folders (and files as well)

#Force to remove folder and sub-folders without warning.
rm -rf folderName
#Remove multiple folders
rm -rf folder1 folder2 folder3
rm -rf aa, rm -rf b c
rm -rf aa, rm -rf b c

Using “rm” and “find” command to do conditional deleting

  • Find a single folder within current folder, then delete it.
find . -type d -name "folder" -exec rm -rf {} +
Find and Delete single folder
Find and Delete single folder
.Current folder
-type dOnly search folder/dirctory
-nameName of the folder
-exec rm -rfRun rm -rf command, delete the folder
{} +append the command to the end of the rm -rf
  • Delete all empty folders
find . -type d -empty -delete
Find and delete all empty folders
Find and delete all empty folders
-emptyOnly delete empty folders
-deleteDelete all empty folders, including empty sub-folders

Leave a Reply

Your email address will not be published. Required fields are marked *