
If you’ve ever tried to delete a file or directory in Linux using the rm command and saw the error:
rm: cannot remove 'file-or-directory': Device or resource busy
Don’t worry, this is a common issue that simply means that the file or directory you’re trying to remove is currently being used by the system or by a currently running process.
Why This Error Happens
When you see the message “device or resource busy”, it means that the file or directory is currently being used. Linux prevents you from deleting files that are in use to avoid breaking things or causing data loss.
Here are some common reasons why this happens:
- You are trying to delete a directory that your terminal is currently inside.
- A program or process is using the file or directory.
- A device (like a USB drive or network mount) is still mounted and in use.
In this article, we’ll explain why this happens and how you can fix it.
1. Exit the Directory
To find out where you are in the filesystem, you can use the pwd command, which stands for “print working directory,” and it shows your current location.
pwd
If the output shows that you’re inside the folder you want to delete, you need to move out of it first by switching to your home directory (or any other safe location) using the cd command:
cd ~
After you’ve moved out of the directory, you can try deleting it again using the rm
command:
rm -rf /path/to/the/directory
This time, Linux won’t block the command because your terminal is no longer using that directory.
2. Check What Is Using the File or Directory
If Linux says a file or directory is “busy,” it means some program or process is still using it. Before you can delete it, you need to find out what is using it and stop that usage.
To do this, you can use a command called lsof, which stands for “list open files”, it helps you see which processes are holding on to your file or directory.
lsof +D /path/to/directory
It will list all processes that are using files inside that directory. You’ll see output showing which command is using it, the process ID (PID), the user running it, and more.
For example, you might see something like:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME bash 4312 user cwd DIR 8,1 4096 2 /mnt/data
Once you identify them, you can close the associated programs or stop the processes using the fuser command.
fuser -v /path/to/directory
To kill all processes using the directory (use with caution):
fuser -k /path/to/directory
3. Unmount the Device (If It’s a Mounted Directory)
Sometimes, the directory you are trying to delete is actually being used as a mount point, which means that a storage device, such as a USB drive, an external hard disk, or a network share (like NFS), is currently attached (or “mounted“) at that location in the filesystem.
When a device is mounted, Linux treats it like part of the main filesystem, but you can’t delete the mount point directory while the device is still in use. That’s why you get the “device or resource busy” error.
To see if your directory is actually mounted, run:
mount | grep /path/to/directory
If the command shows a line of output with your path, it means the directory is being used as a mount point.
For example, you might see something like this, which means a USB device is mounted at /mnt/usb
.
/dev/sdb1 on /mnt/usb type vfat (rw,nosuid,nodev)
Once you confirm the device is mounted, you need to unmount it before deleting the directory.
umount /mnt/usb
Sometimes, even after trying to unmount, you might still see the “resource busy” message, which means a process is still using the device.
In this case, you can try a lazy unmount, which will detach the device from the filesystem immediately, but wait to fully remove it until all processes stop using it.
umount -l /mnt/usb
If lazy unmount doesn’t work either, and you’re sure nothing important is using the device, you can try a force unmount:
umount -f /mnt/usb
Warning: Forced unmounts can cause data loss if the device is still being written to. Only use this option if you’re absolutely sure it’s safe to do so – for example, if the device is stuck and you just need to remove it.
Once the device is unmounted, go ahead and delete the directory.
rm -rf /mnt/usb
Conclusion
The “device or resource busy” error usually means that something is still using the file or directory. With a few simple checks, such as leaving the directory, checking running processes, or unmounting devices, you can fix the problem quickly.
Always make sure it’s safe to kill processes or unmount devices before you do it. Once that’s taken care of, your rm command should work as expected.