Baeldung Pro – Linux – NPI EA (cat = Baeldung on Linux)
announcement - icon

Learn through the super-clean Baeldung Pro experience:

>> Membership and Baeldung Pro.

No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.

Partner – Orkes – NPI EA (tag=Kubernetes)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

1. Overview

The simplest way of removing files and directories on Linux is by using the rm command. However, sometimes it can end up with an error, such as this one:

$ rm -rf <path>
rm: cannot remove '<path>': Device or resource busy

Here, the options -r and -f mean to force removing any directory under the path. As we can see, the rm command fails to remove the file at the path.

In this tutorial, we’ll learn how to resolve this error. Firstly, we’ll check if another process is using the file we’re attempting to delete. Secondly, we’ll find out if the path is mounting any device.

2. Using the lsof Command

A possible reason for the “device or resource busy” error is that a file in the path is being used by a running process.

To find out if that’s the case, we can use the lsof command.

lsof stands for list open files. It allows us to list any process that uses files under a path. For example, let’s check the path /home/user/test:

$ lsof +D /home/user/test
COMMAND      PID USER   FD   TYPE DEVICE SIZE/OFF     NODE NAME
soffice.b 597622 user   21u   REG   8,17      147 60169512 /home/user/test/file.txt

Here, we’ve used the option +D to list all files under the directory /home/user/test.

As can be seen in the output, there is a file named file.txt that’s being used by another process with the PID (process ID) 597622.

Now, we stop this process by using the kill command with the PID from the above output:

$ kill 597622

The process should now terminate.

Let’s use the lsof command again to ensure no more processes use the files from our path:

$ lsof +D /home/user/test

The output is now empty meaning we’re good to delete the file at the path:

$ rm -rf /home/user/test

This now doesn’t display any error.

3. Unmounting Devices

Another reason why the rm command may cause the “device or resource busy” error is a possible mount of a device under the deleted path.

To verify that, we use the mount command with the grep filter:

$ mount | grep /home/user/test
/dev/sdc1 on /home/user/test type vfat (rw,relatime,uid=1000,gid=1000,fmask=0022,dmask=0022,codepage=437,iocharset=iso8859-1,shortname=mixed,showexec,utf8,flush,errors=remount-ro)

Here, we can see that the device /dev/sdc1 is mounted on the path /home/user/test.

Therefore, we need to unmount the /dev/sdc1 device with the umount command. We might need the sudo access:

$ sudo umount /dev/sdc1

Now, let’s double-check the mount command to ensure we no longer see the mounted devices:

$ mount | grep /home/user/test

The output is empty which is good news. We’re ready to remove the directory at the path without an error:

$ rm -rf /home/user/test

As expected, we no longer see the “device or resource busy” error.

4. Conclusion

In this article, we learned how to resolve the rm command error: “device or resource busy”.

Firstly, we looked at the lsof command to list all the files being used by other processes. By killing those processes, we’re able to resolve the problem.

Secondly, we checked if the path had any hardware device mounted. After the device is unmounted, the rm command no longer fails.