/errors/coreutils/rm-cannot-remove-is-a-directory
rm: cannot remove 'dir': Is a directory
- 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
mkdir build
rm buildOutput · 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:
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:
mkdir -p work/build && cd work
touch build.log build.tmp
rm build*
lsOutput
rm: cannot remove 'build': Is a directory build
To delete only files, filter by type with find:
mkdir -p work/build && cd work
touch build.log build.tmp
find . -maxdepth 1 -type f -name 'build*' -delete
lsOutput
build
Related messages
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.