Use the Terminal to quickly get a Unix path to a file

TerminalIconXWhile you can often access the files you need by browsing the filesystem in the Finder, sometimes you might need to specify the Unix path for a file or folder in question, either to run a Terminal command on it, or to communicate this path to others in an e-mail, online discussion, or otherwise.

There are many ways to get the path of a file in OS X, including the information window or if the file is open then right-clicking its title and seeing its path menu. However, these will not give you a full unix-compliant path to the file.

To do this, you simply need to use the Terminal, which can be used without entering a single command:

  1. Open the Terminal
  2. Drag a file or folder to the Terminal window
  3. Select and copy the auto-completed file path of the item in the Terminal window.

When dragging the item in step 2 above, you can do so from the Finder, but you can also drag form document titles, as well as drag from Spotlight search results. Therefore if you have a file that is open and wish to get the unix path to it, then you can drag the small icon from the top of the window to the Terminal.

Additionally, if you know at least part of the name of a file you wish to access, then you can use Spotlight to locate it and likewise drag it from the Spotlight menu to the Terminal window.

[wpvideo CI3brZ7S]

 

One thought on “Use the Terminal to quickly get a Unix path to a file

  1. Strod

    A couple of comments:

    – Well, I didn’t know you can drag items from the Spotlight menu… that alone makes the hint invaluable.

    – Dragging to the Terminal has the side effect of “protecting” special characters. For example spaces in the path are preceded by a backslash ( \ ). That is sometimes awesome and sometimes inconvenient. (I personally prefer to enclose paths in quotes so special characters don’t need protection, but YMMV).

    – Personally, I need the path to one or more files frequently throughout a typical day. Because of that I have a small Automator service that allows me to right click on a selected icon or group of icons in the Finder, select the service from the contextual menu, and the full paths to all the files are copied to the Clipboard, one per line. It even works for search results that may be located in different folders.

    I think the current version of that service was suggested by Topher in MacFixIt: the workflow receives “files or folders” in “Finder.app”, and the only action is “Copy to Clipboard”. It couldn’t be simpler.

    Unfortunately that version doesn’t work on Snow Leopard: it returns the classic Mac style paths that start with the name of the hard disk and have the directories separated by colons. Some of my Macs at work are stuck with the spotted cold cat, so for them I have an alternate service that runs a six-line shell script to achieve the same results. If any pre-Mountain Lion user wants it, post a reply and I’ll copy the shell script for you.

Comments are closed.