Introduction
Linux provides several commands for managing files and directories. The rmdir command is a simple utility specifically designed to remove directories that are empty.
Unlike commands that can remove files or directories containing data, rmdir is more cautious. It will not remove a directory if it contains files or subdirectories. This makes it useful when you want to clean up unused directory structures without accidentally deleting their contents.
The rmdir command is commonly used by Linux beginners, developers, system administrators, and DevOps professionals for basic filesystem management.
What Is the Rmdir Command in Linux?
rmdir stands for remove directory.
It is used to delete one or more empty directories from the Linux filesystem.
The basic syntax is:
rmdir directory_name
For example:
rmdir old_folder
If old_folder is empty, Linux removes it.
If the directory contains files or other directories, the command normally returns an error instead of deleting its contents.
Why Use Rmdir?
The main advantage of rmdir is its restrictive behavior.
When you use:
rmdir my_folder
Linux will remove the directory only when it is empty.
This provides an additional safety layer when cleaning up directories because you cannot accidentally remove the files inside the directory using rmdir.
It is therefore useful for:
Removing unused empty directories
Cleaning temporary directory structures
Managing project folders
Automating filesystem cleanup
Learning basic Linux file management
Remove an Empty Directory
The simplest example is:
rmdir test
If test is empty, it will be removed.
You can verify the directory structure with:
ls
before and after running the command.
Remove Multiple Empty Directories
You can provide multiple directory names:
rmdir folder1 folder2 folder3
Linux attempts to remove each specified directory.
All of the directories must be empty for the operation to succeed.
Remove Parent Directories
The -p option allows you to remove a directory and its empty parent directories.
For example:
rmdir -p project/src/test
If test is empty, Linux removes it. If src becomes empty as a result, it can also be removed, followed by project if it becomes empty.
This can be useful for cleaning up an entire empty directory hierarchy.
For example:
project/
└── src/
└── test/
If all directories are empty, the command can remove the hierarchy from the bottom upward.
Use Verbose Mode
The -v option displays information about what rmdir is doing.
For example:
rmdir -v test
You may receive output indicating that the directory was removed.
Verbose mode can be useful when working with several directories or troubleshooting scripts.
Ignore Non-Empty Directories
The rmdir command does not provide a standard option to forcefully delete a non-empty directory.
For example:
rmdir my_folder
may return an error similar to:
rmdir: failed to remove 'my_folder': Directory not empty
This is expected behavior.
If you intentionally need to remove a directory and everything inside it, rm -r is the command normally used:
rm -r my_folder
Because recursive deletion can permanently remove many files, use it carefully.
Rmdir vs RM
One of the most important differences between rmdir and rm is what they can remove.
rmdir is intended for empty directories.
rmdir folder
rm can remove files, and with recursive options it can remove directories and their contents.
rm -r folder
For simple cleanup, rmdir is safer when you want to ensure that no files are deleted.
Check Whether a Directory Is Empty
Before using rmdir, you can inspect a directory:
ls folder
If the directory contains no entries, you can remove it:
rmdir folder
You can also use:
ls -la folder
to display hidden files.
This is important because a directory may appear empty in a normal ls listing while still containing hidden files.
Remove Directories With Spaces in Their Names
If a directory contains spaces, enclose its name in quotes:
rmdir "old project"
Alternatively, escape the spaces:
rmdir old\ project
Using quotes is generally easier to read and maintain.
Use Absolute Paths
You can remove an empty directory by providing its complete path:
rmdir /home/user/project/temp
This can be useful when working from another directory.
You can check your current location with:
pwd
and inspect the contents with:
ls
Use Relative Paths
You can also specify a directory relative to your current location:
rmdir ./temp
or:
rmdir ../temp
Here, . represents the current directory and .. represents the parent directory.
Common Rmdir Errors
Directory Not Empty
One of the most common errors is:
rmdir: failed to remove 'folder': Directory not empty
This means the directory contains files or subdirectories.
Check its contents:
ls -la folder
If you want to preserve the contents, remove or move them first.
No Such File or Directory
You may see:
rmdir: failed to remove 'folder': No such file or directory
This means Linux cannot find the specified directory.
Check your current location:
pwd
Then verify the directory name:
ls
Linux filenames are case-sensitive, so Folder and folder are different names.
Permission Denied
You may receive:
rmdir: failed to remove 'folder': Permission denied
This generally means you do not have sufficient permissions to modify the parent directory.
If appropriate and authorized, you may need administrative privileges:
sudo rmdir folder
However, sudo should not be used automatically. First understand why the permission is required.
Rmdir and Hidden Files
A directory can appear empty but still contain hidden files.
For example:
ls folder
might show nothing.
Use:
ls -la folder
to check for hidden entries.
If hidden files exist, rmdir will not remove the directory.
This behavior helps prevent accidental removal of directories containing data that is not immediately visible.
Rmdir in Shell Scripts
The command can also be used in shell scripts to clean up known empty directories.
For example:
#!/bin/bash
rmdir /tmp/example
You can combine it with conditional logic:
if rmdir /tmp/example 2>/dev/null; then
echo "Directory removed"
else
echo "Directory could not be removed"
fi
This can be useful when you want a script to remove a directory only if it is empty.
Rmdir for Project Cleanup
During software development, projects can accumulate empty directories.
For example:
project/
├── src/
├── tests/
├── temp/
└── old/
If temp and old are no longer required and are empty, you can remove them:
rmdir temp old
This provides a straightforward way to clean up directory structures without recursively deleting project data.
Best Practices When Using Rmdir
Follow these practices when using the command:
Check your current directory with pwd.
Verify the target directory with ls.
Use ls -la when hidden files may exist.
Use rmdir when you specifically want to remove empty directories.
Use -p carefully when removing directory hierarchies.
Use -v when you need confirmation of the operation.
Avoid replacing rmdir with rm -r unless recursive deletion is actually required.
Be especially careful when using sudo.
Double-check absolute paths before running cleanup commands.
Test destructive commands in a safe environment when using them in scripts.
Frequently Asked Questions
What does rmdir mean in Linux?
rmdir means remove directory. It removes empty directories from the filesystem.
Can rmdir delete files?
No. rmdir is designed to remove directories, and it does not delete files inside them.
Can rmdir remove a non-empty directory?
No. If a directory contains files or subdirectories, rmdir normally fails.
What command removes a non-empty directory?
The rm -r command can recursively remove a directory and its contents:
rm -r directory
Use it carefully because the operation can delete data permanently.
What is the difference between rmdir and rm?
rmdir removes empty directories, while rm removes files and can recursively remove directories when used with the appropriate option.
Conclusion
The rmdir command in Linux is a simple and safe utility for removing empty directories. Its restrictive behavior makes it particularly useful when you want to clean up directory structures without deleting files accidentally.
Understanding rmdir is an important part of learning Linux filesystem management. Once you are comfortable with rmdir, commands such as mkdir, ls, cd, cp, mv, and rm can help you manage files and directories more efficiently.
Solace Infotech's sitemap includes rmdir among its Linux command resources, alongside other Linux file-management and administration topics.