CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / File & Directory Management / rmdir

rmdir

Remove empty directories

Beginnerfilesystemcleanupsafe

rmdir deletes a directory only if it is completely empty. That restriction is the point: it can never take files with it, so it's the safe way to tidy up empty folders left behind by deploys, builds and log cleanups.

Syntaxrmdir [OPTION]... DIRECTORY...

Watch it run

You want to tidy up directories without any risk of deleting files by accident.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 5
rmdir old-build

old-build was empty, so it's gone. No prompt, no output.

Anatomy of a command

$
commandrmdir

Remove empty directories. Fails with 'Directory not empty' otherwise.

How it works

rmdir calls the rmdir() system call. The kernel checks that the directory contains nothing but . and .. and refuses with ENOTEMPTY otherwise, even if the only thing inside is a hidden file like .gitkeep. It then removes the name from the parent, drops the parent's link count (its .. reference is gone) and frees the inode once nothing has it open. You need write and execute permission on the parent directory, not on the directory being removed. You can't remove a directory that is a mount point (EBUSY), and removing the directory a process is standing in still works: that process is left in a deleted directory. With -p, rmdir repeats the call on each parent in the given path, stopping at the first one that isn't empty.

Options you'll actually use

-pAlso remove each parent in the path once it's empty
-vPrint each directory removed
--ignore-fail-on-non-emptyExit 0 when a directory isn't empty (for scripts)
rm -drm's equivalent of rmdir for a single empty directory
find -type d -empty -deleteRemove all empty directories in a tree

Common mistakes

Using rm -rf to remove what you believe is an empty directory

If it isn't empty (a forgotten .env, an upload), rm -rf deletes it silently.

rmdir dir (fails loudly if anything is inside)
Assuming a directory is empty because ls shows nothing

ls hides dot files (.gitkeep, .env, .cache), and rmdir counts them.

ls -A dir to see everything
rmdir /mnt/data to 'unmount' a disk

A mount point is busy: rmdir fails (Device or resource busy) and you wouldn't want it to remove the mount anyway.

sudo umount /mnt/data, then rmdir if the empty mount point is no longer needed

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Create ~/tmp1/a/b/c (empty), then remove the whole chain with a single rmdir command.

  2. 2

    Create ~/box with a hidden file .keep inside. Try rmdir, see the error, find the cause, and fix it.

  3. 3

    List every empty directory under your home.

Related commands

rmRemove files and directoriesmkdirCreate directoriesfindSearch the filesystem by name, size, age, owner and moreumountDetach a mounted filesystem safely
PreviousmkdirNexttouch
Linux / File & Directory Management / rmdir

rmdir

Remove empty directories

Beginnerfilesystemcleanupsafe

rmdir deletes a directory only if it is completely empty. That restriction is the point: it can never take files with it, so it's the safe way to tidy up empty folders left behind by deploys, builds and log cleanups.

Syntaxrmdir [OPTION]... DIRECTORY...

Watch it run

You want to tidy up directories without any risk of deleting files by accident.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 5
rmdir old-build

old-build was empty, so it's gone. No prompt, no output.

Anatomy of a command

$
commandrmdir

Remove empty directories. Fails with 'Directory not empty' otherwise.

How it works

rmdir calls the rmdir() system call. The kernel checks that the directory contains nothing but . and .. and refuses with ENOTEMPTY otherwise, even if the only thing inside is a hidden file like .gitkeep. It then removes the name from the parent, drops the parent's link count (its .. reference is gone) and frees the inode once nothing has it open. You need write and execute permission on the parent directory, not on the directory being removed. You can't remove a directory that is a mount point (EBUSY), and removing the directory a process is standing in still works: that process is left in a deleted directory. With -p, rmdir repeats the call on each parent in the given path, stopping at the first one that isn't empty.

Options you'll actually use

-pAlso remove each parent in the path once it's empty
-vPrint each directory removed
--ignore-fail-on-non-emptyExit 0 when a directory isn't empty (for scripts)
rm -drm's equivalent of rmdir for a single empty directory
find -type d -empty -deleteRemove all empty directories in a tree

Common mistakes

Using rm -rf to remove what you believe is an empty directory

If it isn't empty (a forgotten .env, an upload), rm -rf deletes it silently.

rmdir dir (fails loudly if anything is inside)
Assuming a directory is empty because ls shows nothing

ls hides dot files (.gitkeep, .env, .cache), and rmdir counts them.

ls -A dir to see everything
rmdir /mnt/data to 'unmount' a disk

A mount point is busy: rmdir fails (Device or resource busy) and you wouldn't want it to remove the mount anyway.

sudo umount /mnt/data, then rmdir if the empty mount point is no longer needed

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Create ~/tmp1/a/b/c (empty), then remove the whole chain with a single rmdir command.

  2. 2

    Create ~/box with a hidden file .keep inside. Try rmdir, see the error, find the cause, and fix it.

  3. 3

    List every empty directory under your home.

Related commands

rmRemove files and directoriesmkdirCreate directoriesfindSearch the filesystem by name, size, age, owner and moreumountDetach a mounted filesystem safely
PreviousmkdirNexttouch