rmdirRemove empty directories
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.
rmdir [OPTION]... DIRECTORY...You want to tidy up directories without any risk of deleting files by accident.
rmdirRemove empty directories. Fails with 'Directory not empty' otherwise.
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.
-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 directoryfind -type d -empty -deleteRemove all empty directories in a treeUsing rm -rf to remove what you believe is an empty directoryIf 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 nothingls hides dot files (.gitkeep, .env, .cache), and rmdir counts them.
ls -A dir to see everythingrmdir /mnt/data to 'unmount' a diskA 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 neededTry these on a Linux machine of your own. Each task has a hint and a solution.
Create ~/tmp1/a/b/c (empty), then remove the whole chain with a single rmdir command.
Create ~/box with a hidden file .keep inside. Try rmdir, see the error, find the cause, and fix it.
List every empty directory under your home.
rmdirRemove empty directories
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.
rmdir [OPTION]... DIRECTORY...You want to tidy up directories without any risk of deleting files by accident.
rmdirRemove empty directories. Fails with 'Directory not empty' otherwise.
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.
-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 directoryfind -type d -empty -deleteRemove all empty directories in a treeUsing rm -rf to remove what you believe is an empty directoryIf 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 nothingls hides dot files (.gitkeep, .env, .cache), and rmdir counts them.
ls -A dir to see everythingrmdir /mnt/data to 'unmount' a diskA 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 neededTry these on a Linux machine of your own. Each task has a hint and a solution.
Create ~/tmp1/a/b/c (empty), then remove the whole chain with a single rmdir command.
Create ~/box with a hidden file .keep inside. Try rmdir, see the error, find the cause, and fix it.
List every empty directory under your home.