18

In many cases "lsof" is not installed on the machines that with I have to work, but the "function" of lsof would be needed very much (ex. on AIX). :\

Are there any "lsof" like applications in the non-windows world?

UPDATE: for ex.: I need to know that what processes use the "/home/username" directory?

  • 1
    Could you be more specific, please? What systems apart from AIX (which is definitely supported by lsof) do you have in mind? Or is there only one specific type of use of lsof you have in mind? Generally: why not lsof? – rozcietrzewiacz Aug 12 '11 at 6:25
  • I'm using Linux 2.6.18-92.el5 GNU, and I don't have lsof, nor do I have the capability to use lsof :( – SSH This Jan 11 '13 at 17:52
  • Related question: how to check open file without lsof – Stéphane Gourichon Jun 27 '17 at 15:43
16

I know of fuser, see if is available on your system.

  • what do i have to give fuser as parameter to list all open files on a system? :O ty! – LanceBaynes Aug 12 '11 at 8:08
  • 2
    The command sudo fuser -vm / 2>&1 | awk '$3 ~ /f|F/' | less can show all processes having open files on the filesystem mounted on /. See man page for specific help. – enzotib Aug 12 '11 at 8:18
  • TY! it gives output like this: pastebin.com/raw.php?i=2z19g6Rk - i googled for it, but i can't find any way to output ex.: "gnome-screensaver", not "gnome-screensav" - so how can i output it with wide command names? :O – LanceBaynes Aug 12 '11 at 8:29
  • it gives the same output for: pastebin.com/raw.php?i=Fe3EJvUv – LanceBaynes Aug 12 '11 at 8:33
  • Something like this: sudo fuser -vm / 2>&1 | awk '$3 ~ /f|F/' | while read user pid flags rest; do printf '%10s %10s %10s %s\n' $user $pid $flags "$(</proc/$pid/cmdline)"; done | less – enzotib Aug 12 '11 at 8:33
15

The Unix Rosetta Stone is a good resource for this kind of questions. It mentions a few alternatives for lsof (see below). Do not however that lsof is the de facto standard application for what it does.

If all you want is to find the process ID(s) that have a particular file open, then you can use fuser on any POSIX-compliant system.

On operating systems with a /proc directory, you can query the files open by a process (the reverse from lsof's most common mode of operation) through information in /proc. Some operating systems have commands for that:

8

If you happen to run Solaris, an alternative to lsof, which isn't installed by default and might choke on ZFS, is pfiles.

eg:

pfiles /proc/*

1

My version, with only a little less running of utilities:

for proc_pid in $(find /proc -maxdepth 1 -name "[0-9]*"); do \
    ls -l ${proc_pid}/fd 2>/dev/null \
    | grep -q "$search_term" \
    && echo "${proc_pid#/proc/}"; \
done

Admittedly, it may not handle all corner cases, but is working in my use-case.

0

This should do the trick. It'll give the all the file descriptor mappings except those which:

  • you don't have permission to view, or
  • are for files which contain the string "Permission denied"
( find /proc -mindepth 1 -maxdepth 1 \
  | grep -E [0-9]+ | xargs -n 1 -I% find %/fd \
  | xargs ls -l \
  | grep -v "Permission denied" ) 2>/dev/null \
| cut -d' ' -f12- | less

If you already know you only care about the mappings for certain programs, you could instead use something more along the lines of:

exec=sshd
pgrep "$exec" | xargs -n 1 ps -p
pgrep "$exec" | xargs -n 1 -I% find /proc/%/fd | xargs ls -l | cut -d' ' -f12- | less

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.