≡ Menu

5 Practical Linux fuser Command Examples

The fuser utility in Linux is a powerful tool. As the name suggests it gives information about file user or the process that is currently using the file or directory.

But fuser functionality is not just limited to giving information about the process. The article explains how to use fuser utility with 5 practical examples.

1. Who is Using a File or Directory?

This is the basic use of fuser command. i.e to Identify which processes are using a particular file or directory.

$ fuser  .
./:                   3965c  4175c  4281c  4334c  4337c

In the above example we used the fuser utility to find all the processes using the current directory ‘./’ .

We see that the output consists of process IDs of the processes using fuser but all the PIDs are followed by a character ‘c’. This indicates the type of access. The type of access can be any one of the following:

  • c      current directory
  • e      executable being run
  • f      open file. f is omitted in default display mode
  • F      open file for writing. F is omitted in default display mode
  • r      root directory
  • m      mmap’ed file or shared library

So ‘c’ in the output would mean that these processes are using this directory as their current directory.

Use Option -v to display detailed information in the output:

$ fuser -v ./
           USER        PID ACCESS COMMAND
./:       himanshu   3965 ..c..    bash
          himanshu   4175 ..c..    gedit
          himanshu   4281 ..c..    bash
          himanshu   4334 ..c..  socket_serv
          himanshu   4337 ..c..    bash

So we see above that running fuser on the current directory gives the information on all the processes that are using this directory.

2. fuser on an Executable

socket_serv is a C program executable which is a TCP server that listens on a particular port.

$ ./socket_serv

When you execute fuser on this executable, you’ll see the following:

$ fuser -v socket_serv
                     USER        PID    ACCESS       COMMAND
socket_serv:         himanshu   4334    ...e.        socket_serv

The access specifier in this example is ‘e’. Its different from the access specifier we saw in above examples. It conveys that the file is an executable.

3. Check Processes Using TCP/UDP Sockets

Using fuser we can also check the processes using TCP/UDP sockets. Since the above stated socket_serv sample C program executable is running on TCP port 5000, lets use fuser utility on this socket.

$ fuser -v -n tcp 5000
                       USER        PID ACCESS COMMAND
5000/tcp:            himanshu   4334   F....  socket_serv

So we see that fuser gives all detailed information of the process running on TCP port 5000.

Other than the examples above, we can use the ‘-m’ flag with this utility to display processes using a mounted file system like a USB drive.

4. Kill Processes that are Using a particular Program

Till now we learned that fuser provides information on the processes using files, directories, sockets etc. But the power of this utility is not restricted to providing information only. You can also kill processes using  this utility.

We saw that a TCP server is running on the system which is accessing the binary file ‘socket_serv’. Now, lets try to kill the process using this binary file using fuser.

$ fuser -v -k socket_serv
                      USER        PID ACCESS COMMAND
socket_serv:         himanshu   4334 ...e.   socket_serv

Notice that we have used the ‘-k’ flag to kill the process using file ‘socket_serv’. Lets see on the other terminal where the server was running.

$ ./socket_serv
Killed

We already explained you how you can kill a process using 4 different methods. Now you know one more method to kill a process.

5. Interactively Kill Processes using fuser

In the above example we saw that the flag ‘-k’ is used when we want to kill the processes using a particular file but to avoid the killing of processes accidentally, another option ‘-i’ exists. If we use this option then ‘fuser’ will run in interactive mode and will ask before killing the process. See the example below.

$ fuser -v -k -i socket_serv
                      USER        PID ACCESS COMMAND
socket_serv:         himanshu   5643 ...e.   socket_serv
Kill process 5643 ? (y/N) y

So we see that using ‘-k’ and ‘-i’ we can selectively kill processes using a particular file.

Now, this is a very powerful use of ‘fuser’ command.

Suppose you want to delete a file forcefully but it is being used by many processes then the processes won’t let you delete the file. In that case, you can use fuser utility to kill all the processes (or selected processes) that are using that file.

$ fuser -v -k -i ./
           USER        PID ACCESS COMMAND
./:       himanshu   3965 ..c..    bash
          himanshu   4175 ..c..    gedit
          himanshu   4281 ..c..    bash
          himanshu   4334 ..c..  socket_serv
          himanshu   4337 ..c..    bash
Kill process 3965 ? (y/N) y
Kill process 4175 ? (y/N) y
Kill process 4281 ? (y/N) y
Kill process 4334 ? (y/N) y
Kill process 4337 ? (y/N) y

Note that the use of ‘-k’ sends a SIGKILL signal to the processes using that particular file. We can use the option -SIGNAL to send any other signal. The list of signals supported by fuser is given by:

$ fuser -l
HUP INT QUIT ILL TRAP ABRT IOT BUS FPE KILL USR1 SEGV USR2 PIPE ALRM TERM
STKFLT CHLD CONT STOP TSTP TTIN TTOU URG XCPU XFSZ VTALRM PROF WINCH IO PWR SYS
UNUSED

For example, the following sends SIGHUP instead of SIGKILL, when you use the -k option.

$ fuser -v  -k -HUP -i ./
Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • Bilal February 20, 2012, 3:47 am

    Nice article.

  • Joe February 20, 2012, 4:40 am

    Great Article. This is a great tool to have in the box.

    Thank You !

  • kumarbka February 20, 2012, 8:39 am

    its really helpful.. Thank you very much.. 🙂

  • Himanshu February 21, 2012, 12:07 am

    Thank you all 🙂

  • bob February 21, 2012, 10:39 am

    might be useful sometimes just state what the command spells out as so that we can recall it easily. In this case fuser is actually “file user”.

  • Himanshu Arora February 22, 2012, 9:35 am

    @bob :
    “As the name suggests it gives information about file user”
    I think it was already there.
    Thanks for the suggestion anyways.

  • Júlio Hoffimann Mendes February 23, 2012, 11:18 am

    Nice article! It’s a really useful tool.

    Regards,
    Júlio.

  • Rod March 2, 2012, 3:14 pm

    This command is really useful. This article about it is just awesome. Thanks a lot! You definitely rock!

  • RG March 3, 2012, 1:11 am

    Thank you for sharing all this !
    Knowledge is power 😉
    “a french reader”

  • sudheer May 23, 2012, 3:17 pm

    Really helpful

  • David Ramirez October 3, 2013, 3:26 pm

    Very good tutorial, thank you for sharing.
    The command however seems to fall short if the open file is exported via NFS and it is an external host who has it grabbed (e.g. editing with vim). I test this with fuser and it doesn’t report any file being open at the time.

  • Gowhar March 8, 2015, 11:00 pm

    Informative

  • Tushar February 4, 2016, 1:00 am

    i used fuser -km /mnt and my server shutted down …now i cant login to server using ssh
    What to do?