UNIX Delete / Remove Directory Command

in Categories , , , , , last updated April 4, 2009

How do I delete / remove a directory under UNIX operating systems?

You need to use rmdir command to remove a directory. It will remove the directory entry specified by each dirname operand, which must refer to an empty directory.

For example, to remove a directory called “cppcode”, enter:
rmdir cppcode

A directory must be empty before it can be removed. Use rm command to remove all files from a directory:
cd cppcode
rm *.cpp
cd ..
rmdir cppcode

You can also recursively remove directories and subdirectories. The directory will be emptied of files and removed using rm command:
rm -r oldletters

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

Share this on (or read 20 comments/add one below):

20 comment

  1. if you do rm -fr cppcode, it will forcefully delete the directory(making it empty and delete).


  2. Is there any way to delete directory recurisively,,without deleting content of each directory .

    Have a question? Post it on our forum!