4/24/2016 - 9:09 AM

Find out which is using a file

Find out which is using a file

# http://www.thegeekstuff.com/2012/08/lsof-command-examples/

# List processes which opened a specific file
lsof /path-of-a-file

# List opened files under a directory (+D will recurse the sub directories also)
lsof +D /path-of-a-directory

# List opened files based on process names starting with (You can give multiple -c switch on a single command line)
lsof -c ssh -c init

# List files opened by a specific user
lsof -u username

# List all open files by a specific process
lsof -p PID

# Kill all process that belongs to what you find out (-t returns process id list)
kill -9 `lsof -t options`

# Execute lsof in repeat mode (-r will continue to list, delay, list until a interrupt is given. +r will end when no open files are found.)
lsof /path-of-a-file -r5

# List all network connections
lsof -i

# List processes which are listening on a particular port
lsof -i :port-number

# List all TCP or UDP connections
lsof -i tcp
lsof -i udp

# List all Network File System(NFS) files
lsof -N

# -a option can combine list options using AND