Skip to content

rmdir Command on ChromeOS Linux Environment

The rmdir command is a simple yet powerful tool for removing empty directories in the Linux environment. On ChromeOS, with the Linux (Crostini) feature enabled, rmdir is an efficient way to clean up your directory structure by safely removing unused folders.


Syntax

The basic syntax of the rmdir command is:

bash rmdir [options] directory_name

Key Points:

  • The target directory must be empty for rmdir to work.
  • Attempting to remove a non-empty directory results in an error.

Examples

Removing a Single Empty Directory

To remove an empty directory named test_dir:

bash rmdir test_dir

Attempting to Remove a Non-Empty Directory

If test_dir contains files or subdirectories, you’ll receive an error:

```bash rmdir test_dir

Output: rmdir: failed to remove 'test_dir': Directory not empty

```

To remove non-empty directories, use rm -r instead.

Removing Multiple Empty Directories

You can specify multiple directories to remove in one command:

bash rmdir dir1 dir2 dir3

Each directory listed must be empty.

Removing Parent Directories

The --parents option allows you to remove a directory and its empty parent directories:

bash rmdir --parents parent_dir/child_dir

This removes child_dir first, then removes parent_dir if it becomes empty.


Options

Commonly Used Options

  • --ignore-fail-on-non-empty: Prevents rmdir from displaying errors if directories are not empty. bash rmdir --ignore-fail-on-non-empty dir1 dir2

  • --verbose: Provides detailed output of the command’s actions. bash rmdir --verbose dir1

  • --parents: Removes the specified directory and its empty parent directories. bash rmdir --parents parent/child


Troubleshooting

Directory Not Empty Error

If you attempt to remove a non-empty directory:

bash rmdir: failed to remove 'directory': Directory not empty

Solution: Ensure the directory is empty by checking its contents:

bash ls directory

Remove any files or subdirectories before retrying:

bash rm -r directory_name

Permission Denied Error

If you lack permissions to remove a directory:

bash rmdir: failed to remove 'directory': Permission denied

Solution: Use sudo to gain elevated permissions:

bash sudo rmdir directory_name


Best Practices

  • Double-check the Directory: Before removing, ensure the directory is no longer needed.
  • Avoid Non-Empty Directories: Use rmdir only for empty directories to avoid unintended data loss.
  • Combine with Other Tools: Pair rmdir with find to locate and remove empty directories: bash find . -type d -empty -exec rmdir {} \;

Alternatives

If the target directory is not empty, use the rm command with the -r option:

bash rm -r directory_name