fuser

list process IDs of all processes that have files open

OS X:
fuser [ -cfu ] file
Linux:
fuser [-fMuvw] [-a|-s] [-4|-6] [-c|-m|-n space] [-k [-i] [-signal]] name...

Writes to standard output the process IDs of processes running on the local system with files open.
      For block special devices (like disk partitions, sockets), all processes using any file on that device are listed.

Writes to standard error how the process is using the file :
    c current directory
    r root 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.
    m mmap'ed file or shared library.

OS X:

-c file is treated as a mount point and reports any files open in the file system.
-f only the named files.

-u user name (or UID), written to STDERR

Linux:

-a
--all
Show all files specified on the command line.
 > fuser -u /bin/bash /usr/bin/fuser
/bin/bash:            4895
default: only files that are accessed by at least one process are shown. not with -s.
> fuser -a /bin/bash /bin/fuser
/bin/bash:            4895
/bin/fuser:
-s
--silent
--user and -verbose are ignored .
Return Code: non-zero if none of the specified files is accessed or fatal error.
-m name
--mount name
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, it is automatically changed to NAME/. to use any file system mounted on that directory.

-M
--ismountpoint
only if Name specifies a mountpoint.
Prevents killing the machine if NAME happens to not be a filesystem.
-c Same as -m
-k
--kill Kill
signal processes accessing the file. KILL is default
-w Kill only processes which have write access.
-i
--interactive
for confirmation before killing a process.
-SIGNAL signal Default KILL name (e.g. -HUP).
-l
--list-signals
List signal names.
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
-n space
--namespace space
Different name space:
  • file names, the default
  • udp (local ports)
  • tcp (local ports) For ports, either the port number or the symbolic name can be specified.
    If there is no ambiguity, the shortcut notation name/Ispace (e.g. 80/tcp ) can be used.
  • -u
    --user
    user of the process owner to stderr
    -v
    --verbose
    Use ps-like style. PID, USER and COMMAND are similar to ps. ACCESS shows how the process accesses the file.
    When a particular file is being access as a mount point, knfs export or swap file kernel is shown instead of the PID.
    > fuser -v -m /dev/mmcblk0p5 
                         USER        PID ACCESS COMMAND
    /dev/mmcblk0p5:      root     kernel swap  /dev/mmcblk0p3
                         root     kernel mount /dev
                         dgerman    2438 f.... xstartup
                         dgerman    2451 f.... ck-launch-sessi
                         dgerman    2502 f.... lxsession
                         dgerman    2506 F.... dbus-daemon
                         dgerman    2512 f.... openbox
                         dgerman    2524 f.... lxpolkit
                         dgerman    2527 F.... gvfsd
                         dgerman    2532 F.... pulseaudio
                         dgerman    2557 F.... gvfs-gphoto2-vo
                         dgerman    2561 F.... menu-cached
                         dgerman    2564 F.... gvfsd-trash
    
     > fuser -v .
                         USER        PID ACCESS COMMAND
    /home/dgerman:       dgerman    2427 ..c.. Xtightvnc
                         dgerman    2438 ..c.. xstartup
                         dgerman    2451 ..c.. ck-launch-sessi
                         dgerman    2512 ..c.. openbox
                         dgerman    2516 ..c.. pcmanfm
                         dgerman    4895 ..c.. bash 
    -V
    --version
    Display version information.
    fuser (PSmisc) 22.19
    Copyright (C) 1993-2010 Werner Almesberger and Craig Small
    -4
    --ipv4

    -6
    --ipv6

    Search only for IPv4/6 sockets. and only has an effect with the tcp and udp namespaces.
    -f ignored
    - Reset all options and set the signal back to SIGKILL.

    When standard output and standard error are directed to the same destintion, the output is interleaved so that the filename appears at the start of each line, followed by the process ID and characters indicating the use of the file.

    Returns a non-zero return code if none of the specified files is accessed or in case of a fatal error

    EXAMPLES

    From DaPie (raspberry Pi)

    DaPie: > sudo fuser -m /dev/mmcblk0p5
    /dev/mmcblk0p5:        157   282   286  1540  1549  1828  1907  1911  1940m  
    
    DaPie:/ > sudo fuser -u -m /dev/mmcblk0p5 
    /dev/mmcblk0p5:        157(root)   282(root)   286(root)  1540(root)  1549(root)  1828(root)  1907(root)  1911(root)  1940m(root)  
    
    
    > fuser -u  2>&1 |sed "s/)/)\n/g"   #make seperate lines