List Open Files By User

The command lsof  lists open files.  However, even if you use the -u option to show files open by just the user, there is a lot of files used in the background that you don’t really know about.  I wanted to parse this down to get just the files I had open in the GUI.

Below is the command I came up with to give me a concise list of files I have open:

lsof -u your_username | awk '{print substr ($0, index($0, $5))}' | grep "/Users/your_username/" | grep -v "DIR" | grep -v "/Library/" | grep -v "/Applications/" | grep -v "/.dropbox/" | uniq | awk -F'/' '{print $NF}' | sort

Here is how the command breaks down with each pipe:

  • -u  option to get files open only by the user your_username
  • print only the 5th field and everything after it (the 5th column shows the filetype)
  • grep  just the files from the home folder
  • grep  out any directories because we want only the files
  • grep  out any files in the Library since they are probably just open in the background
  • grep  out any files in the Applications folder since they are being used by the application in the background
  • grep  out the hidden Dropbox file, which is using files you don’t see in the GUI
  • get only the uniq ue entries
  • print only the filename by using the “/” character as a delimiter and then only printing the last field, which is the filename
  • sort  the list alphabetically for easy reading

Leave a Reply