Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Drag & Drop Finder Items Into Terminal for Full Path

· 5 min read · Home Tech

In this article
  1. Why This Works: How macOS Handles the Drag
  2. How to Drag and Drop: Step-by-Step
  3. What You Get: Absolute vs. Relative Paths
  4. Common Problems and How to Fix Them
  5. Alternatives: Copying Paths and Using Shortcuts
  6. Why This Is Useful: Real-World Examples
  7. Frequently Asked Questions

Yes, you can drag and drop files or folders from Finder into the Terminal to insert their full path automatically. This is a built-in macOS feature that has worked for decades, and it’s one of the quickest ways to reference a file without typing or copying a path manually.

Why This Works: How macOS Handles the Drag

When you drag an item from Finder and drop it onto a Terminal window, macOS doesn’t move the file—it converts the file’s location into a text string and pastes it at the cursor position. The system uses the file’s POSIX path, which is the standard Unix-style path starting from the root directory (/).

For example, if you drag a file named report.pdf from your Downloads folder, the Terminal will receive:

/Users/yourusername/Downloads/report.pdf

The path is automatically quoted if it contains spaces or special characters, so you don’t need to worry about escaping them yourself. This works in the built-in Terminal app and in most third-party terminal emulators like iTerm2.

How to Drag and Drop: Step-by-Step

The process is simple, but there are a few nuances depending on what you want to do with the path.

  1. Open the Terminal app (found in Applications > Utilities).
  2. Position the Terminal window so you can also see the Finder window containing the file or folder you need.
  3. Click and hold the item in Finder, drag it into the Terminal window, and release it at the command prompt.
  4. The full path appears immediately, ready for you to add a command before or after it.

For instance, to open a file with a text editor, you could drag the file after typing open:

open /path/to/file.txt

Or to change to a folder’s directory, drag the folder after typing cd:

cd /path/to/folder

You can also drag multiple items at once—they’ll be inserted one after another, separated by spaces. This is handy for commands that accept multiple arguments, like cat file1 file2 or diff file1 file2.

What You Get: Absolute vs. Relative Paths

The path you get is always absolute, meaning it starts from the root directory and includes every parent folder. This is different from a relative path, which is relative to your current working directory in the Terminal.

For example, if your current directory is /Users/yourname and you drag a file from your Desktop, you’ll get:

/Users/yourname/Desktop/file.txt

Even though you could have typed Desktop/file.txt as a relative path, the absolute path is unambiguous and works regardless of where you are in the filesystem. This is especially useful in scripts or when you’re not sure what directory the Terminal is currently in.

Common Problems and How to Fix Them

While drag-and-drop is reliable, a few issues can arise:

  • Path appears with backslashes before spaces: In some terminal emulators, spaces are escaped with backslashes (e.g., My ile.txt). This is valid and works the same as quoting. If you prefer quotes, you can replace the backslashes manually or use a different terminal.
  • File doesn’t drop: If you’re using a full-screen Terminal or a window manager that intercepts drags, try resizing the window or using a different terminal app. Also ensure you’re dropping onto the text area, not the title bar.
  • Path is pasted with a trailing space: Some systems add a space after the path. This is harmless; just press Backspace if you don’t want it.
  • Dragging from certain apps like Spotlight: Spotlight results don’t always support drag-to-terminal. Use Finder instead.

Alternatives: Copying Paths and Using Shortcuts

If dragging isn’t convenient, there are other ways to get a full path in Terminal:

  • Copy as Pathname: In Finder, right-click (or Control-click) a file, hold the Option key, and select Copy “file” as Pathname. This copies the absolute path to the clipboard, which you can then paste into Terminal with Cmd+V.
  • Drag into the Terminal’s title bar: In some versions of Terminal, dragging a file onto the title bar changes the working directory to that folder. This isn’t a path insertion, but it can be useful for context.
  • Use the pwd command: If you’re already in a folder and need its path, type pwd to print the current working directory.
  • Use find or mdfind: For files not easily found, you can use mdfind -name filename (Spotlight) or find / -name filename to locate them, though these are slower.

Why This Is Useful: Real-World Examples

Drag-and-drop saves time and reduces typos. Here are common scenarios where it shines:

  • Editing config files: Instead of typing nano /etc/hosts, you can drag the hosts file from Finder (if you can see it) or from a Finder window showing the root directory.
  • Running scripts on files: For example, python script.py followed by a dragged file path.
  • Copying files with cp or mv: Drag the source and destination to avoid path errors.
  • Compressing or decompressing: tar -czf archive.tar.gz then drag a folder.
  • Checking file details: file, ls -l, or mdls on a dragged file.

Frequently Asked Questions

Does drag-and-drop work in iTerm2 or other terminals?

Yes, most terminal emulators for macOS support drag-and-drop of files. iTerm2, for example, inserts the path with proper quoting. If a terminal doesn’t support it, you can usually copy the path from Finder and paste it.

Can I drag a folder to use it as the working directory?

Yes, you can drag a folder after cd to change to that directory. Alternatively, dragging a folder onto the Terminal window’s title bar (in the default Terminal app) changes the working directory without typing cd.

Why does the path have spaces escaped with backslashes?

That’s the shell’s way of handling spaces. When you paste a path with backslashes, the shell interprets them as escaping the space, so the path is treated as one argument. If you prefer quotes, you can remove the backslashes and wrap the path in quotes.

Can I drag a file from the Desktop if I’m in a different directory?

Absolutely. The absolute path works from any directory. That’s the main advantage over typing a relative path, which depends on your current location.

Is there a way to drag a file and have it automatically run a command?

Not directly, but you can create a small AppleScript or use a tool like “cd to” (a terminal add-on) that lets you drag a folder to a button. For a single command, you’d still need to type the command before or after the dragged path.

Does this work with files on external drives or network volumes?

Yes, as long as the file is mounted and visible in Finder. The path will include the mount point, such as /Volumes/ExternalDrive/file.txt. Network volumes may have longer paths, but drag-and-drop still works.