How find out which process is using a file in Linux?

后端 未结 3 1919
生来不讨喜
生来不讨喜 2020-12-02 10:32

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 ca

3条回答
  •  余生分开走
    2020-12-02 10:56

    @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.

提交回复
热议问题