Several weeks ago, I discovered a neat trick, thanks to Rob “That’s not a hint; this is a hint” Griffiths. On his Mac OS X Hints weblog, he discussed a terminal command called lsof (short for “list open files”). His suggestion was to use this to find out what application had a file open when you receive this irritating message while trying to empty the Trash: “The operation cannot be completed because the item * is in use.”
If you open up a Terminal session, you can type lsof and it will tell you what files are open. This is usually a pretty long, unhelpful list, but if you add the path and name of the file you’re looking for at the end, for example lsof /Users/dmoren/Desktop/example.txt, it’ll print out just the entry for that file. If you want to see a list of all files open for a particular application, you can pair it with grep, i.e. lsof | grep Safari (the “|” character is the one above your return key; in the command line, it represents a way to take the output of one application and send it into another application).
But that’s not the end of lsof use. Today, I was trying to determine what network port a particular application was using. I did a quick search for solutions on the net, and what should come up, but our old friend lsof? Turns out if you type lsof -i, you’ll get a list of open ports, along with other network info. Piping that through grep makes it pretty easy to find what port you need.
MacUser is your source for news, info, and opinion about Apple, the Mac, and the iPod. Our dedicated team of bloggers covers everything that is relevant to Mac users — and, okay, some stuff that’s not quite relevant, but is still a lot of fun.
Leave a comment