Finding open files

Have you ever run into a situation where you can’t eject a drive or open a file because another application has that file open? With the right tools it is easy to quickly find who has the file open so that you can take appropriate action and get on with your work. Why wait for 10 minutes while your PC reboots when you can solve the problem in a minute or less with the right tools?

Windows

There are several options for finding open files and the process which has them open. The easiest is to use the Microsoft published tool Process Explorer. This tool is available on the Sysinternals.com website here . Let’s look at an example on how this tool helps us solve the open file problem.

  1. Install Process Explorer or run it by navigating to http://live.sysinternals.com/procexp.exe . This live link allows you to run Process Explorer on any web connected Windows PC without having to install it.
  2. Launch Process Explorer and navigate to View in the menu.
    1. Ensure the “Show Lower Pane” is enabled
    2. Set the lower pane view to “Handle”
  3. Either type “Ctrl – F” or navigate to “Find->File Handle or DLL…” in the Process Explorer menu
  4. In the search box enter in any part of the file name. If the name is common, it may make it easier to enter in part of the path as well if that is known. For example if I have a file called sources that is being held open, I can search for the word “sources” or if I know it lives in a directory called explore_asm, I can search for that directory name instead since it is less common than the name “sources”.
  5. Process Explorer will search all processes to see who has a file open with that pattern in its full path. The results are displayed in the lower table (if any results are found). Double clicking an entry in the table will take you to the open file and process in the Process Explorer main window. You can then choose to close the process or navigate to it and close the file from within the application itself. You can close it manually (using the right click menu) in the handle view but this is not recommended since it can cause crashes in the application holding the file open.
    1. A note about permissions. If you run Process Explorer in a non administrative account , it will only show processes and files from applications you own. You must run Process Explorer with administrative privileges (Run as Administrator) if you want to check all applications on your computer. You can also start Process Explorer normally then navigate to “File->Show Details for All Processes” in the Process Explorer menu.

Linux/OS X

On most modern UNIX based OS’, the “lsof” tool is installed by default. If this tool is not already installed you can download and build it from ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof . Building requires an installed compiler and patience as well as administrative rights to install. Preferably get pre-built binaries from the OS provider or via a package manager such as yum .

  1. Open your favorite terminal
  2. Enter the command: lsof | grep -i “file name or part of path here”
    1. Example output:
      vim       88732 briant    4u     REG               14,2     12288 39132857 /Users/briant/.test.c.swp
  3. The process name and its process ID occupy the first two columns in the output. With this information you can kill the process or navigate to it and close the file from within the process.

Further Reading

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Your email address will not be published. Required fields are marked *