Ubuntu – Copying file path from GUI to the command line

command linecopy and pasteshortcut-keys

When I copy a file path from the GUI (right-click on file and press 'Copy') and then paste it in the command line (Ctrl + Shift + V), it shows as:

file:///home/user/Documents/file.ext

I would like it to show as:

/home/user/Documents/file.ext

Is there a shortcut for this? I work a lot with images and it is a bit cumbersome to always manually delete 'file://'.

Best Answer

As of Files (nautilus) 3.32, the clipboard operation has changed to accommodate the Gnome Shell "Desktop Icons" extension. As a result, copying a file will fill the clipboard with

x-special/nautilus-clipboard
copy
file:///home/user/Documents/299867.jpg

rather than, like before, just the path name:

/home/user/Documents/299867.jpg

Power users thus can no longer conveniently copy paste the pathname from Files to a terminal or editor, or a File - Open/Save dialog.

The issue has been filed with Gnome. You can work around by installing either a Nautilus Python extension or a Nautilus script.

Nautilus Python extension

+ Nicely integrated + Quick right-click mouse access - No hotkey

If just using the mouse to copy the path is OK for you, it is sufficient to install the python extension nautilus-copypath. It provides you with a right-click menu item to copy the path, but does not provide the ability to achieve that with a hotkey.

Nautilus script

+ Hotkey access - Two clicks required - Less "integrated"

Power users prefer to keep their hands on the keyboard, though. While Python extensions are neatly integrated, there is no easy way (that I discovered) to assign them a keyboard shortcut or at least a menu accelerator key. In contrast, one can easily assign a hotkey to a nautilus script. The drawback is that one more click is required to select the entry with the mouse. To have both optimal keyboard access and optimal mouse access, you can always install both the script and the Python extension.

1. Create a nautilus scripts folder. If not yet available, create a nautilus scripts folder under .local/share/nautilus. To see the hidden .local folder in Files, select "Show hidden files" (or press Ctrl+h). Any executable scripts you put in that folder will appear in a "Scripts" menu that will appear in your right-click menu of Files.

2. Create a script to copy the file path. Open your text editor, paste following text

#!/bin/bash
echo -n "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" | perl -pe 'chomp if eof' | xsel -b -i

The perl construct is there to remove the last hard return.

Save the text file in the nautilus scripts folder (.local/share/nautilus/scripts) with the name Copy Path. You can choose any file name, but be aware that the file name is what will appear as entry under your Scripts menu. Close your editor.

3. Make the script executable. Right-click the script file in Files, select "Properties" (or just press Alt+Enter with the file selected) and on the "Permissions" tab, check "Execute:".

4. Add a hotkey for the function. With your text editor, open, or create if it does not yet exist, a file .config/nautilus/scripts-accels (i.e., a file named script-accels in the folder nautilus under the hidden folder .config. Add a line to define the hotkey you want to use to copy the pathname. I use Ctrl+Shift+c, so I added a line as:

<Control><Shift>c Copy Path

Close that file. You need to fully restart Files for the script to become available.

5. Make sure xsel is installed. xsel is a command line tool to manipulate the contents of the clipboard. It is not installed by default, so you may need to install it for the nautilus script to work: sudo apt install xsel.