How to get over "device or resource busy"?

I tried to rm -rf a folder, and got “device or resource busy”.

In Windows, I would have used LockHunter to resolve this. What’s the linux equivalent? (Please give as answer a simple “unlock this” method, and not complete articles like this one. Although they’re useful, I’m currently interested in just ASimpleMethodThatWorks™)

Asked By: ripper234

||

The tool you want is lsof, which stands for list open files.

It has a lot of options, so check the man page, but if you want to see all open files under a directory:

lsof +D /path

That will recurse through the filesystem under /path, so beware doing it on large directory trees.

Once you know which processes have files open, you can exit those apps, or kill them with the kill(1) command.

Answered By: camh

I use fuser for this kind of thing. It will list which process is using a file or files within a mount.

Answered By: BillThor

sometimes it’s the result of mounting issues, so I’d unmount the filesystem or directory you’re trying to remove:

umount /path

Answered By: kip2

Here is the solution:

  1. Go into the directory and type ls -a
  2. You will find a .xyz file
  3. vi .xyz and look into what is the content of the file
  4. ps -ef | grep username
  5. You will see the .xyz content in the 8th column (last row)
  6. kill -9 job_ids – where job_ids is the value of the 2nd column of corresponding error caused content in the 8th column
  7. Now try to delete the folder or file.
Answered By: user73011

I had this same issue, built a one-liner starting with @camh recommendation:

lsof +D ./ | awk '{print $2}' | tail -n +2 | xargs -r kill -9
  • awk grabs the PIDs.
  • tail gets rid of
    the pesky first entry: "PID".
  • xargs executes kill -9 on the PIDs. The -r / --no-run-if-empty, prevents kill command failure, in case lsof did not return any PID.

I had this problem when an automated test created a ramdisk. The commands suggested in the other answers, lsof and fuser, were of no help. After the tests I tried to unmount it and then delete the folder. I was really confused for ages because I couldn’t get rid of it — I kept getting "Device or resource busy"!

By accident I found out how to get rid of a ramdisk. I had to unmount it the same number of times that I had run the mount command, i.e.
sudo umount path

Due to the fact that it was created using automated testing, it got mounted many times, hence why I couldn’t get rid of it by simply unmounting it once after the tests. So, after I manually unmounted it lots of times it finally became a regular folder again and I could delete it.

Answered By: gloriphobia

If you have the server accessible, Try

Deleting that dir from the server

Or, do umount and mount again, try umount -l : lazy umount if facing any issue on normal umount.

I too had this problem where

lsof +D path : gives no output

ps -ef : gives no relevant information

Answered By: Prabhat Kumar Singh

Riffing off of Prabhat’s question above, I had this issue in macos high sierra when I stranded an encfs process, rebooting solved it, but this

ps -ef | grep name-of-busy-dir

Showed me the process and the PID (column two).

sudo kill -15 pid-here

fixed it.

Answered By: bil

I experience this frequently on servers that have NFS network file systems. I am assuming it has something to do with the filesystem, since the files are typically named like .nfs000000123089abcxyz.

My typical solution is to rename or move the parent directory of the file, then come back later in a day or two and the file will have been removed automatically, at which point I am free to delete the directory.

This typically happens in directories where I am installing or compiling software libraries.

Answered By: user5359531
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.