Deleting a directory in Linux is easy; choosing the right command for what’s inside it takes a little more care. rmdir stops if anything remains. rm -r deletes a directory tree, so a typo or unexpected path can remove more than you intended.
Choose the command by what you want removed
For an empty directory, use rmdir:
rmdir dirname
It removes the directory only if it is empty. Hidden files count as contents, so a directory that looks empty in a regular ls listing may still make rmdir fail.
For a directory that contains files or subdirectories, use recursive removal:
rm -r dirname
The -r option tells rm to descend into the directory and delete its contents before removing the directory itself. This is permanent from the command line, so inspect the target first if there’s any doubt.
pwd
ls -la dirname
pwd confirms your current location; ls -la shows the target’s contents, including hidden files. This is especially useful over SSH, where a familiar directory name might exist in more than one place.
What -f changes—and what it doesn’t
You’ll often see this command:
rm -rf dirname
-r makes the removal recursive. -f suppresses confirmation prompts and ignores missing paths. It does not make rm recursive by itself: rm -f dirname still won’t remove a directory containing files.
Use -f only when you intend to skip prompts. If you want to review each removal as it happens, use interactive mode instead:
rm -ri dirname
Be particularly cautious with commands assembled from variables or wildcards. In a script, check that a path variable is set and points where you expect before using it in a destructive command. Don’t rely on rm’s safeguards to catch every dangerous path construction.
There’s also a symlink detail worth knowing: if dirname is a symbolic link to a directory and you want to remove just the link, use rm dirname without a trailing slash. A trailing slash can make the path resolve through the link and act on the target’s contents. When you’re unsure what a path refers to, inspect it before deleting.
Empty several directories, or clear one in place
rmdir can remove a chain of directories from the end upward, but only while each one is empty:
rmdir -p parent/child/grandchild
It tries grandchild, then child, then parent, and stops if a directory still contains something.
If you want to remove several directory trees, pass their paths to one command:
rm -r dir1 dir2 dir3
Each path is processed independently; an error on one does not necessarily prevent the others from being processed. Check every path before running the command.
Sometimes the goal is to empty a directory while keeping the directory itself—for example, because its ownership or permissions should remain unchanged. Use find for that:
find dirname -mindepth 1 -delete
-mindepth 1 skips the starting directory and selects its contents. This also catches hidden files, unlike a simple dirname/* shell glob, which normally skips names beginning with a dot.
When deletion fails
If rmdir reports that the directory is not empty, inspect it with ls -la dirname. If you do intend to remove its contents, switch to rm -r rather than trying to force rmdir.
If rm says “Is a directory,” you used it without -r. Use rmdir for an empty directory or rm -r when you intend to remove a tree.
A “Permission denied” error may be about the parent directory, not the directory you’re deleting. Removing a directory entry requires appropriate permissions on its parent. Check the parent’s permissions and your current user:
ls -ld .
whoami
id
The sticky bit can add ownership restrictions in shared directories such as /tmp. If you need a refresher on what permission changes mean, this guide to changing file permissions in Linux explains the basics. Avoid broad fixes such as chmod 777; they can make a directory writable by everyone.
Shell commands and SFTP are different
If you’re connected with an SFTP client rather than an interactive shell, don’t assume shell commands work there. SFTP has its own command set: its rmdir removes an empty remote directory, while removing a non-empty tree may require shell access or a recursive-delete feature in your client. The OpenSSH SFTP command reference covers the distinction and the available commands.
Before a recursive delete, verify the location and target. Use rmdir when you want Linux to refuse if anything remains; use rm -r only when you mean to delete the directory and its contents. Reserve -f for cases where suppressing prompts and ignoring missing paths is intentional.
I originally published a more detailed version of this guide on the SSHFlow blog.
I'm also building SSHFlow — an SSH client where every server gets its own workspace for terminals, SFTP, code, and databases.
Top comments (0)