Commands.page Logo

How to Delete a Directory Recursively in Ubuntu

This article provides a concise guide on removing a folder and all nested files within the Ubuntu operating system. It covers the specific terminal command required for recursive deletion, explains the necessary flags, and highlights essential safety warnings to avoid irreversible data loss during system management.

The Command Structure

To delete a directory and all its contents, use the rm command with the -r and -f flags. The -r option stands for recursive, allowing the removal of folders and subfolders. The -f option stands for force, which suppresses confirmation prompts and ignores non-existent files.

The basic syntax is:

rm -rf directory_name

Replace directory_name with the path to the folder you wish to remove.

Practical Examples

To delete a folder named projects located in your current directory, run:

rm -rf projects

To delete a folder using its absolute path, provide the full directory location:

rm -rf /home/user/documents/old_backups

Using Sudo for Protected Directories

If the directory belongs to the root user or requires administrative privileges, you must prepend sudo to the command. This elevates your permissions to perform the deletion:

sudo rm -rf /var/www/html

Enter your user password when prompted. The terminal will not display characters while you type the password.

Critical Safety Warnings

The rm -rf command permanently deletes data without sending it to the trash bin. This action cannot be undone. Always verify the directory path before pressing Enter. Avoid running this command as root unless absolutely necessary, and never execute rm -rf / as it will destroy your entire operating system. Double-check your typing to ensure you are targeting the correct folder.