How do I remove a directory in Unix?

In Unix, you can remove a directory using the rmdir command. Here’s an example:

rmdir directory_name

This will remove the directory directory_name if it is empty. If the directory is not empty, you will receive an error message that the directory is not empty.

To remove a directory and all of its contents, including subdirectories and files, use the rm command with the -r (recursive) option:

rm -r directory_name

This will remove the directory directory_name and all of its contents. Be careful when using the rm command with the -r option, as it can permanently delete files and directories.

Note that some Unix systems may have the rm command aliased to include the -i (interactive) option by default, which will prompt you for confirmation before deleting each file. To avoid this, you can unalias the rm command before running the above command:

unalias rm
rm -r directory_name

Leave a Comment