3

I know there can be a lot of duplicates. I have gone through almost all of them (like lsof, fuser, etc) and I could not get it resolved.

rm: cannot remove 'temp_p/sys': Device or resource busy

This is the error I receive while trying to delete a folder from a bash script. The command I use is:

test -f "$TEMP_DIR" && sudo rm -rf "$TEMP_DIR"

I tried to do rm -rf from the shell, and it shows the same output. Output of lsof is:

lsof +D <directory name>

lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/76585/gvfs Output information may be incomplete. lsof: WARNING: can't stat() vfat file system /media/kostef1/9016-4EF8 Output information may be incomplete.

Fuser also does not show anything. If I list all hidden files, it still hoes not show anything starting with dot.

1 Answers1

1

It suggests temp_p/sys has a filesystem mounted on it, you'd need to unmount it first:

umount -- "$TEMP_DIR/sys"
rm -rf -- "$TEMP_DIR"

Or:

if mountpoint -q -- "$TEMP_DIR/sys"; then
  umount -- "$TEMP_DIR/sys" || exit
fi
rm -rf -- "$TEMP_DIR"

To check whether it is a mount point first, and abort if it can't be unmounted.

BTW, your rm -rf will have cleaned the contents of the filesystem mounted there, which the above avoids.

To empty $TEMP_DIR, leaving alone file on separate file systems, you can use find:

find "$TEMP_DIR" -xdev -depth -delete

(-delete implies -depth, but it's good practice to still include it as a reminder).

Also note that test -f "$TEMP_DIR" is to test whether $TEMP_DIR is a regular file (after symlink resolution), it would return false for any other type of file including directories. Maybe you meant test -d instead. Also note that with -f, rm wouldn't complain if asked to remove a file that is already gone, so it's fine to invoke it on something that may not exist.