How to remove/delete a directory in Linux/macOs


There are multiple ways in which you can remove or delete a directory in Linux or macOS using Terminal commands, let us take a look at some of them,

Remove directory using rmdir command

rmdir is a command line utility that you can use to delete empty directories, this will work only when the directory is empty, let's see some examples,

 % mkdir myDir     
 % rmdir myDir
 % 

In the below example we created a directory and add a file in it, now if you try to delete the directory you will get the error "Directory not empty"

 % mkdir myDir     
 % cd myDir 
 % touch myFile.txt
 % cd ..

 % rmdir myDir
rmdir: myDir: Directory not empty
 % 

Remove directory using rm command

If you want to remove a directory that contains directories and files in it you can make use of rm -r command,

 % rm myDir 

rm: myDir: is a directory

 % rm -r myDir
 % 

If you get no error and you are returned to the prompt returns, your command was successfully executed and the dir should be deleted.

remove or delete directory using linux command
-




Have Questions? Post them here!