How to Delete a Directory in Linux

In Linux, you can delete a directory using the rmdir command. The rmdir command stands for “remove directory,” and it is used to delete empty directories. If you want to delete a directory that is not empty, you will need to use the rm command instead.

Here is the basic syntax for using the rmdir command:

rmdir directory

The directory argument is the path to the directory that you want to delete.

For example, to delete the /home/user/documents directory, you would use the following command:

rmdir /home/user/documents

If the /home/user/documents directory is not empty, you will see an error message like this:

rmdir: /home/user/documents: Directory not empty

To delete a non-empty directory, you will need to use the rm command with the -r flag, which stands for “recursive.” The -r flag tells the rm command to delete the specified directory and all of its contents, including subdirectories and files.

Here is the basic syntax for using the rm command with the -r flag:

rm -r directory

For example, to delete the /home/user/documents directory and all of its contents, you would use the following command:

rm -r /home/user/documents

Exercise caution when you are planning to delete a directory in Linux, as the files may not be easily recovered. Ideally you should test the command safely, such as on a test virtual machine, or on a empty directory, before running on an important system.

Related posts

Mastering the Linux ifconfig Command

Docker Exec Command With Practical Examples

Debugging with Git Bisect

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Read More