What is fuser?
The fuser command in linux is a useful tool through which we can identify the process(es) which are using a particular file. The file in question could be a normal file, a directory, an executable etc.
5 fuser Examples
1. Check processes using the current directory
In this example, we will run the fuser command to see which processes are using the current directory.
$ fuser . .: 1562c 1620c 1626c 1627c 1629c 1630c 1631c 1632c 1633c 1655c 1697c 1777c 1824c 1852c 1886c 2380c 2464c
So we see above that a lot of processes are using the current directory (as its the home directory). The numeric value(s) in the output above output represent the PID of the processes using the directory. The alphabet ‘c’ signifies ‘current directory’. Complete list of alphabets that can be part of fuser output include :
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
2. For verbose output use -v
If we wish to have a detailed output then ‘-v’ option can be used. We repeat the same example as above but this time with -v option :
$ fuser -v . USER PID ACCESS COMMAND .: himanshu 1562 ..c.. gnome-session himanshu 1620 ..c.. metacity himanshu 1626 ..c.. polkit-gnome-au himanshu 1627 ..c.. nm-applet himanshu 1629 ..c.. gnome-power-man himanshu 1630 ..c.. mintUpdate himanshu 1631 ..c.. nautilus himanshu 1632 ..c.. gnome-panel himanshu 1633 ..c.. bluetooth-apple himanshu 1655 ..c.. python himanshu 1697 ..c.. syndaemon himanshu 1777 ..c.. python himanshu 1824 ..c.. gnome-terminal himanshu 1852 ..c.. firefox himanshu 1886 ..c.. plugin-containe himanshu 2380 ..c.. bash himanshu 2464 ..c.. vlc
3. Check processes using an executable
Lets consider example of an executable here and see how fuser identifies the processes using an executable. I opted for running fuser on firefox executable which is running on my system :
$ ps -aef | grep firefox himanshu 1852 1 7 06:57 ? 00:06:06 /usr/lib/firefox-11.0/firefox himanshu 1886 1852 14 06:58 ? 00:12:10 /usr/lib/firefox-11.0/plugin-container /usr/lib/flashplugin-installer/libflashplayer.so -greomni /usr/lib/firefox-11.0/omni.ja 1852 true plugin himanshu 2551 2380 0 08:24 pts/1 00:00:00 grep --colour=auto firefox
So the first entry in the output above gives the path to firefox executable running in the system. I’ll use the same path in the following fuser command
$ fuser /usr/lib/firefox-11.0/firefox /usr/lib/firefox-11.0/firefox: 1852e
So we see that the output is the PID of the process (which can be cross checked against the output of the ‘ps’ command above). The post fix ‘e’ signifies that the file is an executable.
4. To output process owner use -u
An extra information regrading the user name of the process owner can also be appended to each PID using the -u option.
$ fuser -uv . USER PID ACCESS COMMAND .: himanshu 2380 ..c.. (himanshu)bash himanshu 2760 ..c.. (himanshu)bash himanshu 2855 ..c.. (himanshu)gcalctool
5. Display information about all files on command line
We can specify multiple files as input to fuser but by default it provides information on the first file it encounters in the input. If we wish to have information on all the files we supplied as input then -a option can be used.
$ fuser ./Desktop/ ./Downloads/ ./Desktop/: 1892c 1894c $ fuser -a ./Desktop/ ./Downloads/ ./Desktop/: 1892c 1894c ./Downloads/:
In the example above, I tried two commands. One without -a option and one with -a option and we can see the difference. The command with -a option displayed information on all the files listed as input to fuser.
Syntax and Options
fuser [-fuv] [-a|-s] [-4|-6] [-c|-m|-n space ] [-k [-i] [-M] [-SIGNAL ] ] name ... fuser -l fuser -V
|Short Option||Long Option||Option Description|
|-a||–all||Show all files specified on the command line. By default, only files that are accessed by at least one process are shown.|
|-c||Same as -m option, used for POSIX compatibility.|
|-f||Silently ignored, used for POSIX compatibility.|
|-k||–kill||Kill processes accessing the file. Unless changed with -SIGNAL, SIGKILL is sent. An fuser process never kills itself, but may kill other fuser processes. The effective user ID of the process executing fuser is set to its real user ID before attempting to kill.|
|-i||–interactive||Ask the user for confirmation before killing a process. This option is silently ignored if -k is not present too|
|-m NAME||–mount NAME||NAME specifies a file on a mounted file system or a block device that is mounted. All processes accessing files on that file system are listed. If a directory file is specified, it is automatically changed to NAME/. to use any file system that might be mounted on that directory.|
|-M||–ismountpoint||Request will be fulfilled only if NAME specifies a mountpoint. This is an invaluable seatbelt which prevents you from killing the machine if NAME happens to not be a filesystem.|
|-v||–verbose||Verbose mode. Processes are shown in a ps-like style. The fields PID, USER and COMMAND are similar to ps. ACCESS shows how the process accesses the file. Verbose mode will also show when a particular file is being access as a mount point, knfs export or swap file. In this case kernel is shown instead of the PID.|
|-SIGNAL||Use the specified signal instead of SIGKILL when killing processes. Signals can be specified either by name (e.g. -HUP) or by number (e.g. -1). This option is silently ignored if the -k option is not used.|