Skip to content

/errors/coreutils/rm-cannot-remove-is-a-directory

rm: cannot remove 'dir': Is a directory

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · ls (GNU coreutils) 8.32updated
tool
GNU coreutils
exit code
1
versions
GNU coreutils 8.x – 9.x
os
linux, macos, windows

rm: cannot remove 'build': Is a directory means you asked rm to delete a directory, and rm deletes only files unless you pass -r. Use rmdir build if the directory is empty, or rm -r build to delete it with everything inside. The command exits with status 1 and leaves the directory in place.

Reproduce the error

bash
mkdir build
rm build

Output · exit 1

rm: cannot remove 'build': Is a directory

Fix: pick the right removal

rmdir and rm -d remove only empty directories, so they cannot delete data by accident. rm -r descends into the directory:

bash
mkdir -p work/empty work/full && cd work
touch full/a.o full/b.o
rmdir empty
ls full
rm -r full
ls
echo "exit status: $?"

Output

a.o
b.o
exit status: 0

A glob that matches a directory

rm build* fails on the directory but still removes the files that matched, so the error can hide a partial success:

bash
mkdir -p work/build && cd work
touch build.log build.tmp
rm build*
ls

Output

rm: cannot remove 'build': Is a directory
build

To delete only files, filter by type with find:

bash
mkdir -p work/build && cd work
touch build.log build.tmp
find . -maxdepth 1 -type f -name 'build*' -delete
ls

Output

build

rmdir: failed to remove 'DIR': Directory not empty means the directory still has entries (including hidden ones, visible with ls -A). rm: cannot remove 'DIR': Directory not empty with -d means the same thing.

Sources