All Tutorials

Your One-Stop Destination for Learning and Growth

How to Delete a Directory in Linux and Mac OS X Terminal

Deleting a directory in Linux or Mac OS X using the terminal may seem intimidating for some, but it's actually quite straightforward. In this post, we'll walk you through the process step-by-step.

Prerequisites

Before we begin, make sure you have the following prerequisites in place:

  1. A working terminal session (Linux or Mac OS X)
  2. Sufficient permissions to delete directories (usually requires being logged in as the root user or having sudo privileges)

Deleting an Empty Directory

Let's start with deleting an empty directory. First, navigate to your terminal and type:

cd /path/to/directory

Replace /path/to/directory with the actual path to the directory you want to delete. Once you are inside the directory, use the following command to remove it:

rmdir name_of_directory

Replace name_of_directory with the name of the directory you wish to delete. If the directory is empty, this command will delete it immediately.

Deleting a Directory with Contents

If the directory contains files or subdirectories, you'll need to use a different approach. First, navigate to the parent directory of the directory you want to delete:

cd ..

Then, use the rm -r command with the -i flag to interactively confirm each deletion:

rm -r -i name_of_directory/

Replace name_of_directory with the name of the directory you wish to delete. The terminal will prompt you for confirmation before deleting each file or subdirectory within that directory.

Conclusion

With these simple steps, you now know how to delete directories in Linux and Mac OS X using the terminal. Remember always to be careful when deleting files and directories, as there's no undo! If you have any questions or need further clarification, please don't hesitate to ask.

Published December, 2017