Unix shell remove directory

October 27, 2007

How do I remove a directory in unix?

Unlike windows, Unix is usually used as a shell environment. This way, the operating becomes tricky and hard if you do not know the commands. Very often you need to remove files/directories. To remove a file you just type rm filename and that’s it. It’s gone .

To remove a directory that you own, use the rmdir command. For example, to remove a directory named mydirectory that is into your working directory you can just type at the unix shell: rmdir mydirectory , or rm ./mydirectory .If mydirectory exists, and is an empty directory, it will be removed. If the directory is not empty or you do not have permission to delete it, you will see an error message.

So how do I delete a directory that is not empty on Unix?

To remove a directory that is not empty you just need to use the rm command and pass it a parameter. That’s the -r switch that will tell os to delete the folder in a recursive manner ( deleting any sub folders and files. So, if you want to delete mydirectory, which is in your working directory and which contains another sub directories, you just need to type rm -r mydirectory. The whole directory will be deleted.

For more information on the rmdir and the rm -r commands, you can consult the man pages that can be found under any Unix distribution!

CAUTION: Be careful while using these commands because you can easily loose precious data.

 

Post a comment

Name (required)

Mail (will not be published) (required)

Website

*
To prove you're a person (not a spam script), type the security text shown in the picture. Click here to regenerate some new text.
Click to hear an audio file of the anti-spam word