I tried to remove a file in Linux using rm -rf file_name
, but got the error:
rm: file_name not removed. Text file busy
How can I find out which process is using this file?
The lsof command (already mentioned in several answers) will tell you what process has a file open at the time you run it. lsof is available for just about every unix variant.
you can use lsof . this command is for find out what processes currently have the file open. if process opening the file, writing to it, and then closing it you can use auditing.
There are a couple of commands which can help us to find processes that operate on files, so we’ll start there. These commands gather data from the Linux kernel because it is responsible for running the processes, file systems, among other things. Additionally, we’ll read the kernel tables directly to get the needed info. 2.1. The fuser Command
In this article, we will explain how to find out who is using a particular file in Linux. This will help you know the system user or process that is using an open file. We can use the lsof command to know if someone is using a file, and if they are, who. It reads kernel memory in its search for open files and helps you list all open files.
The lsof command (already mentioned in several answers) will tell you what process has a file open at the time you run it. lsof is available for just about every unix variant.
To discover PID s of all processes using files in a directory and below, we can recursively scan it with +D. Additionally, lsof is frequently used to find all files opened by a given (by PID) process: 2.3. Getting Information From the Kernel Directly Another way of detecting the process of a file in use is by accessing the kernel directly.
You can use the fuser
command, like:
fuser file_name
You will receive a list of processes using the file.
You can use different flags with it, in order to receive a more detailed output.
You can find more info in the fuser's Wikipedia article, or in the man
pages.
@jim's answer is correct -- fuser
is what you want.
Additionally (or alternately), you can use lsof
to get more information including the username, in case you need permission (without having to run an additional command) to kill the process. (THough of course, if killing the process is what you want, fuser
can do that with its -k
option. You can have fuser
use other signals with the -s
option -- check the man page for details.)
For example, with a tail -F /etc/passwd
running in one window:
ghoti@pc:~$ lsof | grep passwd tail 12470 ghoti 3r REG 251,0 2037 51515911 /etc/passwd
Note that you can also use lsof
to find out what processes are using particular sockets. An excellent tool to have in your arsenal.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With