Linux copy current path

Copy file and folder path from Nautilus

How can I copy the file and folders full path in Nautilus? In the right-click context menu there is no such option provided. In file/folder property window I can only copy the folder path.

10 Answers 10

To quickly get a file path in Nautilus we can use the right click context entry «Copy» to copy the file path to the clipboard.

Then just «Paste» (resp. «Paste Filenames») this path from the clipboard to the other application, e.g. a text editor.

Unfortunately in Ubuntu 12.04 this does not work flawlessly. When I paste to GNOME Terminal I get URL i.e. file:\\. (with URL encoded characters like %20 for space) instead of the ordinary path.

@pabouk as a workaround you can drag and drop files onto the GNOME terminal. This will paste the file path instead of its URI.

This does not work in 20.04.2 (nautilus 3.36.3). When I copy and past, I get x-special/nautilus-clipboard copy file:///. . It used to work in previous versions.

You can use to copy folder path from Nautilus with the command

Unfortunately pasting file path from Nautilus to GNOME Terminal does not work as expected. It pastes the path as an URL with URL encoded characters. For example it pastes:

 file:///etc/gconf/gconf.xml.defaults/%25gconf-tree.xml 
 /etc/gconf/gconf.xml.defaults/%gconf-tree.xml 

Solution with clipboard

Use the Edit > Paste Filenames function from the terminal menu which also takes care of escaping for a shell. Unfortunately this function does not have a keyboard shortcut (besides Alt + E + F ) and it seems that it is not possible to set one using gconf-editor .

Solution with drag & drop

Dragging a file or directory from Nautilus to GNOME Terminal transfers the correctly formatted path like Edit > Paste Filenames mentioned above.

This is resolved in Ubuntu 16.04. Copying a file from Nautilus to GNOME Terminal will just give the path /path/to/file , not the URL.

In Ubuntu 20.04 this is definitely not resolved (or the bug is back?) You get terrible gunk: x-special/nautilus-clipboardcopyfile://file .

I found a solution to this. You can use Nautilus actions utility to add «Copy path» and «Copy directory path» the the context menu.

Source. Don’t forget to make the .py-file executable.

Since Takkat’s solution seems to be broken with Nautilus version >= 3.30 (bug report), I would like to offer a workaround that creates a context menu option.

sudo apt-get install filemanager-actions nautilus-actions xclip

  1. Run FileManager-Actions, and create a new action like so:
  • Hit button «Define a new action»
  • Name the action in the «Items list», e.g. «Copy path to clipboard»
  • Under the «Action» tab, check the two options «Display item in selection context menu», and «Display item in location context menu»
  • Under the «Command» tab, set path to «bash», and «Parameters» to

-c «realpath -z %B | xclip -selection clipboard»

  • Hit the button «save the items tree»
  • restart nautilus: «nautilus -q»
  1. Now you should be able to right click a file/folder, and see the action we just defined to copy the path
Читайте также:  Изменить метку раздела linux

Simply sudo apt-get install pcmanfm , open it, choose ‘keep in Unity starts’ and finally remove Nautilus.

The logo is the same, you won’t notice the difference — except that you can now copy the path..

Nautilus does not provide this possibility.
But it should be possible to achieve this if you write a plugin for Nautilus.

The functionality to copy a file path has changed (i.e., for practical purposes, broken) since 20.04 to accommodate requirements to work with the Desktop Icons Gnome Shell extension. The good news is that the old behaviour, where a copy will place plain file path in the clipboard, is restored in Files 40.0. Files 40 which will make it into Ubuntu 21.10, rendering the accepted answer of this question valid again.

Create the file and make executable

sudo touch /usr/share/nautilus-python/extensions/nautilus-copy-paths.py sudo chmod +x /usr/share/nautilus-python/extensions/nautilus-copy-paths.py sudo gedit /usr/share/nautilus-python/extensions/nautilus-copy-paths.py nautilus -q 

then when editing past i the below content and restart nautilus afterwards

import os from gi import require_version require_version('Gtk', '3.0') require_version('Nautilus', '3.0') from gi.repository import Gdk, Gtk, Nautilus, GObject from gettext import gettext, bindtextdomain, textdomain NAUTILUS_PATH="/usr/bin/nautilus" class NautilusAdmin(Nautilus.MenuProvider, GObject.GObject): """Simple Nautilus extension that adds some file path actions to the right-click menu, using GNOME's new admin backend.""" def __init__(self): pass def get_file_items(self, window, files): """Returns the menu items to display when one or more files/folders are selected.""" # Don't show when more than 1 file is selected if len(files) != 1: return file = files[0] # Add the menu items items = [] self._setup_gettext(); self.window = window if file.get_uri_scheme() == "file": # must be a local file/directory if file.is_directory(): if os.path.exists(NAUTILUS_PATH): items += [self._create_nautilus_item(file)] return items def get_background_items(self, window, file): """Returns the menu items to display when no file/folder is selected (i.e. when right-clicking the background).""" # Add the menu items items = [] self._setup_gettext(); self.window = window if file.is_directory() and file.get_uri_scheme() == "file": if os.path.exists(NAUTILUS_PATH): items += [self._create_nautilus_item(file)] return items def _setup_gettext(self): """Initializes gettext to localize strings.""" try: # prevent a possible exception locale.setlocale(locale.LC_ALL, "") except: pass bindtextdomain("nautilus-admin", "/usr/share/locale") textdomain("nautilus-admin") def _create_nautilus_item(self, file): item = Nautilus.MenuItem(name="NautilusAdmin::Nautilus", label=gettext("Copy path"), tip=gettext("Copy File path to clipboard")) item.connect("activate", self._nautilus_run, file) return item def _nautilus_run(self, menu, file): """'Copy File path' menu item callback.""" uri = file.get_uri() file_path = uri.replace("file://", "") clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD) clipboard.set_text(file_path, -1) clipboard.store() 

Источник

Читайте также:  Qbittorrent установка kali linux

How can I copy the path to the currently opened file in gedit to the clipboard?

I am trying to write a custom command in Gedit which copies the currently open and active document’s path(both upto parent dir & upto file) to clipboard, as I couldn’t find any gedit-plugins or tools that can do this. I have no clue yet as to where to start from, nor have any good references, but I know I have to do scripting in bash script. I searched for external command to copy any string to clipboard from terminal(as it also runs bash script) but the answers suggest use of «xclip» tool, which I have tried and am disappointed as any string when copied with xclip can only be pasted with «xclip -o» command. I need the copied string to be paste-able with Ctrl-V so I can open the path in file manager(nautilus). Any help/suggestion is appreciated.

4 Answers 4

Script to copy the path of a file, opened in gedit

With the gedit window in front, the small script below derives the path from the ( gedit ) window’s name, and copies it to the clipboard.

The script has two options:

    Only copy the path to the file’s directory, running the script with the option

The script

#!/usr/bin/env python3 import subprocess import sys name = subprocess.check_output(["xdotool", "getactivewindow", "getwindowname"]).decode("utf-8").strip() if all(["(" in name, ")" in name]): path = name[name.find("(")+1:name.find(")")] if sys.argv[1] == "-file": fname = name[:name.find("(")] elif sys.argv[1] == "-path": fname = "" command = "echo "+'"'+path+"/"+fname+'"'+" | xclip -selection clipboard" subprocess.Popen(["/bin/bash", "-c", command]) 

How to use

sudo apt-get install xdotool xclip 
sleep 5 && python3 /path/to/get_path.py -file 

enter image description here

immediately switch to the gedit window, to make the last part of tyhe command run with the gedit window in front.

  • Press Ctrl + V somewhere to paste the just copied path.
  • If all works fine, you can make the options available in two ways:
    1. Create two shortcut keys for both options: choose: System Settings > «Keyboard» > «Shortcuts» > «Custom Shortcuts». Click the «+» and add both commands to two different shortcuts.
    2. Make both options available in the gedit launcher: Copy the content below into an empty file, save it as gedit.desktop in ~/.local/share/applications
    [Desktop Entry] Name=gedit GenericName=Text Editor Comment=Edit text files Exec=gedit %U Terminal=false Type=Application StartupNotify=true MimeType=text/plain; Icon=accessories-text-editor Categories=GNOME;GTK;Utility;TextEditor; X-GNOME-DocPath=gedit/gedit.xml X-GNOME-FullName=Text Editor X-GNOME-Bugzilla-Bugzilla=GNOME X-GNOME-Bugzilla-Product=gedit X-GNOME-Bugzilla-Component=general X-GNOME-Bugzilla-Version=3.10.4 X-GNOME-Bugzilla-ExtraInfoScript=/usr/share/gedit/gedit-bugreport Actions=Window;Document;divider1;Copy current file's directory;Copy path+file name; Keywords=Text;Editor;Plaintext;Write; X-Ubuntu-Gettext-Domain=gedit [Desktop Action Window] Name=Open a New Window Exec=gedit --new-window OnlyShowIn=Unity; [Desktop Action Document] Name=Open a New Document Exec=gedit --new-document OnlyShowIn=Unity; [Desktop Action Copy current file's directory] Name=Copy current directory Exec=python3 /path/to/get_path.py -path OnlyShowIn=Unity; [Desktop Action divider1] Name=. OnlyShowIn=Unity; [Desktop Action Copy path+file name] Name=Copy current directory, include file name Exec=python3 /path/to/get_path.py -file OnlyShowIn=Unity; 
    Exec=python3 /path/to/get_path.py -path 
    Exec=python3 /path/to/get_path.py -file 

    replace /path/to/get_path.py by the real path to the script.

    Log out and back in to make Unity «switch» to the new, local .desktop file.

    Explanation

    In the gedit window name, the path is displayed between ( and ) . The script simply sees the frontmost window with the help of xdotool , then reads the path between those two characters.

    Notes

    Since the path is read in a textual way, the script will fail if the file’s name includes other () characters.

    Examples

    With the following window in front:

    enter image description here

    the first option will copy to the clipboard the path to the file:

    while the second option includes the file itself:

    ~/Bureaublad/some test file.txt 

    As you can see, spaces are taken care of :).

    Источник

    How can I copy the current path from Nautilus?

    enter image description here

    In 10.10, when opening a directory in Nautilus, I was wondering how to copy the current path? My address bar, pictured here, is not copyable:

    I suppose that you want to cd to the directory Nautilus is in. A quick way to do that would be to right-click and select open in terminal (as long as your ~/.bashrc doesn’t set your pwd )

    OP doesn’t want to open a terminal there but wants to copy the current location to the clipboard which as the accepted answer shows is really easy.

    8 Answers 8

    I’d say the quickest way is to press Ctrl + L , then you can copy it ( Ctrl + C ).

    enter image description here

    Hehe it’s a feature for sure and I believe it has been implemented for the same reason this question has been asked 😛

    It used to show the location bar all the time, now breadcrumb style navigation is the default. Oh and +1 for keyboard shortcuts!

    And to toggle it back, strangely, you can’t use Ctrl+L. You have to use Esc. (See my answer for other related tips about saving your preference for this, and about «terminal here».)

    Ctrl+L . Very frustrating to not find an option in the View menu (which should then be clearly labeled Ctrl+L). Had to do a web search.

    And then very frustrating to find that Ctrl+L doesn’t toggle it back. Another web search. Drum roll. Esc

    And then, how to set your preference? Web search. Have to install gconf-editor or dconf-editor or manually use a terminal command:

    gsettings set org.gnome.nautilus.preferences always-use-location-entry true 

    Of course, this would be a little less painful if there were a ‘terminal here’ option in the context menu. Web search. Install nautilus-open-terminal

    Sigh. Oversimplified interfaces are so hard to figure out. I appreciate all the helpful answers people have put on the web for us to find.

    Источник

  • Оцените статью
    Adblock
    detector