UNIX Delete / Remove Directory Command

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.

Advertisement

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

Note:
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

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

20 comments… add one
  • Kiruba Oct 3, 2012 @ 6:27

    Thanks. It works.

  • karan Oct 11, 2012 @ 17:21

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

    Thanks,
    KARAN

  • Guru Oct 14, 2012 @ 2:14

    It helped me :) thanks a tone :))))))

  • Harun R Nov 12, 2012 @ 21:11

    You could also do a:

    rm -rf

  • shahebaz Apr 2, 2013 @ 5:38

    thaks bt i want more

  • srinath Apr 19, 2013 @ 13:57

    Thanks it works

  • Abhinandan Jun 10, 2014 @ 7:10

    thanks a lot..it solved my problem in a beautiful way

  • Anand Sep 25, 2014 @ 16:51

    rm -fr also removed forcefully.

  • avinash Mar 9, 2015 @ 11:58

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

Leave a Reply

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

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.