# Deleting Non-Empty Directories in Linux


As a Linux user, you may find yourself needing to delete a directory that contains files or other directories. While deleting empty directories is straightforward, removing non-empty ones requires a bit more care.

In this article, we'll explore some safe methods to delete non-empty directories in Linux.

## Understanding the Risks

When you delete a non-empty directory, you are removing everything within it, including sub-directories and files. If you're not careful, you could end up deleting important data by accident.

Additionally, many Linux systems have access restrictions in place to protect critical folders. Attempting to forcibly delete protected directories as a normal user can result in permissions errors or system instability.

So before we continue, remember:

- **Be cautious** when deleting directories with unknown contents
- **Do not** try to delete system directories as a normal user
- **Back up** anything critical within a directory before deleting

Okay, now that we've got the warnings out of the way, let's look at some techniques for safely deleting non-empty directories.

## Using `rm -r`

The simplest command for deleting a directory and its contents is `rm -r`, short for "remove recursively". For example:

```plaintext
rm -r my_folder
```

This will delete `my_folder` and everything inside it. Simple enough!

However, `rm -r` can be _dangerous_ if you're not careful with the path. It will recursively delete without prompting, so double check you have the right location. Safer alternatives exist, so avoid `rm -r` unless you're confident about what you're deleting.

## Leveraging the `rmdir` Command

The `rmdir` command is safer than `rm -r` since it will _only_ remove empty directories. Attempting to run `rmdir` on a path with contents will output an error like:

```plaintext
rmdir: failed to remove 'my_folder': Directory not empty
```

We can take advantage of this by combining `rmdir` with other commands to safely delete non-empty directories, step-by-step.

For example:

```plaintext
rm -r my_folder/*
rmdir my_folder
```

This first removes all _contents_ of `my_folder` recursively with `rm -r`, leaving an empty directory. `rmdir` can then delete the empty parent directory.

By separating the content and directory deletion steps, `rmdir` gives you a chance to confirm or undo before removing parent folders.

%[https://developnsolve.com/monitoring-the-progress-of-dd-commands-in-linux]

## Leveraging Bash Loops

If you need to remove a deeply nested directory with lots of contents, bash loops can help automate things. This saves you from having to repeat many `rm -r` or `rmdir` commands.

For example, imagine we have a `projects/old_work/experiment1` folder to remove:

```plaintext
for x in experiment1 old_work projects; do
    rm -r "/projects/old_work/$x"*/
    rmdir "/projects/old_work/$x"
done
```

This loop will traverse the directory tree backwards, removing contents first, then the empty parent. The end result deletes the full `projects/old_work/experiment1` branch.

Loops let you cleanly automate complex delete workflows without missing steps or introducing human errors.

## Using the `find` Command

The `find` tool locates files or folders based on criteria you specify. We can use it before deleting directories.

For instance, this command deletes all empty subfolders over a week old under `my_folder`:

```plaintext
find my_folder/ -type d -empty -mtime +7 -exec rmdir {} \;
```

Breaking it down:

- `find my_folder/` - Search under my_folder
- `-type d` - Only match directories
- `-empty` - Only select empty ones
- `-mtime +7` - Whose last modification was over 7 days ago
- `-exec rmdir {} \;` - Then delete them using rmdir

Using `find` lets you _confirm_ exactly what will get deleted before pulling the trigger.

## Review Deletions First with `ls`

The simple `ls` command also helps avoid unwanted data loss.

For example, before any deletions:

```plaintext
ls -R /path/to/target_directory
```

The `-R` flag recursively lists all contents of the directory and sub-folders.

Review the output to ensure you know what will get removed. If something looks critical, stop and double check if it should be preserved first!

## Proceeding with Confidence

While there is always risk when deleting data, armed with the right Linux commands, you can safely delete non-empty directories. By fully understanding what will be removed before acting, unintended loss can be avoided.

The key is adopting commands like `rmdir`, `find`, and `ls` for planned, validated removal, rather than blunt forced deletion with `rm -r`. Yes, deleting with reckless `rm -r` is quicker, but like any powerful tool, restraint and wisdom should be applied.

As you build Linux skills, periodically review directories earmarked for potential clean-up. Over time unnecessary folders and content accumulate. By proactively removing what is obsolete, systems stay better organized.

Approach each deletion deliberately, confirm what goes, and understand the command’s exact behavior before executing. Working this diligence into your admin habits will prevent many head-scratching "where did that data go?" moments down the road.

