-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfsof.sh
More file actions
46 lines (45 loc) · 1.37 KB
/
fsof.sh
File metadata and controls
46 lines (45 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# fsof - friendly lsof wrapper (files open for/by...)
# Usage: fsof listen who is listening on what IPv4 port
# fsof <port> what process is using this port
# fsof <file> what process has this file open
# fsof <dir> what processes have files open in this dir
# fsof <pid> what files this process has open
# fsof net all network connections
fsof() {
local arg="${1:-}"
if [[ -z "$arg" ]]; then
echo "Usage: fsof <listen|port|file|dir|pid|net>" >&2
return 1
fi
case "$arg" in
listen)
local out
out=$(lsof -i4 -P -n | grep LISTEN)
if [[ -z "$out" ]]; then
echo "fsof: no IPv4 listeners found"
else
echo "$out"
fi
;;
net)
lsof -i -P -n
;;
[0-9]*)
if kill -0 "$arg" 2>/dev/null; then
lsof -p "$arg"
else
lsof -i ":${arg}" -P -n
fi
;;
*)
if [[ -d "$arg" ]]; then
lsof +D "$arg"
elif [[ -e "$arg" ]]; then
lsof "$arg"
else
echo "fsof: '$arg' not found — use listen, a port number, file, dir, pid, or 'net'" >&2
return 1
fi
;;
esac
}