GREP-LSOF-Terminal.png

Have you ever tried to eject a disk or shared server, only to be told that it could not be ejected because a file was in use on that disk? How annoying.

Here is a terminal command that generates a list of all the files open on that specific drive.

lsof | grep /Volumes/Workspace

Note: You must replace /Volumes/Workspace with the path to the specific drive that is refusing to eject. Read on for further details.

The Mac OS X Terminal Application.

TerminalApp-Icon.pngWe don’t run a lot of terminal commands in this general tips blog, so I’ll go into more detail on this tip than many people are going to require.

In order to enter any terminal commands, you’ll have to first launch the Terminal application, which can be found in your Utilities folder.

Diagram of a simple GREP Search.

Let’s look at this terminal command in detail. There are four parts to this particular search.

GREP-LSOF-Diagram.png

lsof — lsof is a command meaning “list open files. Try typing this command by itself in the Terminal, and you’ll be rewarded with a long list of currently open files. That list includes hundreds of system files used by Mac OS X. It might be interesting to look at, but there are far too many files open at one time to make this useful by itself.

GREP-Pipe-Character.pngPipe Character ( | ) — The pipe character in a terminal command instructs your computer to feed the results from the first command into a second one. In this case using this character after the lsof command tells your computer to feed the list of open files into the grep search we’re about to discuss.

grep — grep is a powerful text-search utility that has been a part of most Unix installations for years. In this case, we are using grep to limit our list of open files to only those results that include the path to our disk in question.

Path to Disk — Finally, we want to limit our search to files that reside on our target disk. Here is the easiest way to make sure the path is correct. After typing the first part of your command, just drag the disk icon from the Finder into your Terminal window. Terminal will automatically drop in the full path name for you.

GREP-DragVolume.gif

Put this all together, and you get this:

lsof | grep /Volumes/Workspace

In English: Get a list of all the open files, and show me a list of those results that are on this particular volume.

Looking at the results.

GREP-Results.png

Here are the results from our example. I’ve highlighted the resulting filename in yellow, which shows that a Word document named “ACME Copywriting.doc” is currently open — preventing me from ejecting my disk.

In other cases you may find that Photoshop has active files in a TemporaryItems folder; which indicates that Photoshop is using your volume as a scratch disk. Designers can often find that fonts are currently open on their volumes. Some detective work may be required, but this trick can help you discover what open files are getting in the way.

Tip: System Administrators can use this same technique on a Mac OS X Server to determine which users have open files on a particular share point.

Source: At Creativetechs we use versions of this technique in a wide range of troubleshooting situations. The timing of this tip was inspired by a discussion of GREP features found in InDesign CS3 and InDesign CS4.