Skip to content

Instantly share code, notes, and snippets.

@VanTanev
Last active December 20, 2015 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VanTanev/6091505 to your computer and use it in GitHub Desktop.
Save VanTanev/6091505 to your computer and use it in GitHub Desktop.
#!/bin/bash
# List all opened files using only the /proc fs.
main() {
pids | file_descriptor_folder | xargs ls -l | remove_ls_total_lines | remove_ls_file_attributes | remove_non_files | less
}
pids() {
# we try all pids from 1 to the pid max and echo out the valid ones
for P in $(seq 1 $(cat /proc/sys/kernel/pid_max)); do
[ -d "/proc/$P" ] || continue
echo $P
done
}
file_descriptor_folder() {
# we build the folder name for the file descriptors for each pid
sed -r 's/(.*)/\/proc\/\1\/fd/'
}
remove_ls_total_lines() {
# remove file count from ls' result
grep -v 'total'
}
remove_ls_file_attributes() {
# remove information about the link attributes and leave only the dereferenced link path
sed -r 's/^.*->/->/'
}
remove_non_files() {
# remove everything that is not an actual file - stuff like sockets and so on
grep -v -e '-> \w'
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment