Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This kills every process with a handle to file /foo/bar (in bash):

 lsof /foo/bar 2>&1 | grep "/foo/bar" | sed "s/  */\\t/g" | cut -f 2 | while read PID; do kill $PID; done

This does not seem like such an uncommon task that there wouldn't be an easier solution, so I'm wondering if there's something like killall or a switch to kill that I've missed which does the same.

share|improve this question
up vote 3 down vote accepted

That's what -t is for. The man page even suggests you'd use that for kill.

lsof -t /some/file | xargs kill

Traditionally (before the lsof days), you'd use:

fuser /some/file 2> /dev/null | xargs kill

for that.

Some fuser implementations, like the one found on most Linux-based operating systems , Solaris or recent FreeBSDs can even do the killing by themselves:

fuser -k /some/file

However note that they send a SIGKILL, not SIGTERM. You can choose a different signal with -TERM with some implementations and -s TERM with others.

share|improve this answer
Nice - also, I didn't know about xargs. – Johannes Bauer Feb 12 '14 at 9:09

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.